小编Vas*_*ash的帖子

从 JUnit4 迁移到 JUnit5 会在 @Autowired 存储库上引发 NullPointerException

我有一个非常简单的存储库测试,当我使用 JUnit 的 4“@RunWith(SpringRunner.Class)”时它运行得很好。当我尝试在提供的示例中使用“@ExtendWith”时,在尝试使用存储库时出现 NullPointerException。使用后一个注释时,“@Autowire”似乎没有注入存储库。这是 pom.xml 文件和堆栈跟踪:https : //pastebin.com/4KSsgLfb

实体类:

package org.tim.entities;

import lombok.AccessLevel;
import lombok.Data;
import lombok.NonNull;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

@Entity
@Data
public class ExampleEntity {

@Id
@Setter(AccessLevel.NONE)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@NonNull
private String name;
Run Code Online (Sandbox Code Playgroud)

}

存储库类:

package org.tim.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.tim.entities.ExampleEntity;

@Repository
public interface ExampleRepository extends JpaRepository<ExampleEntity, Long> {
}
Run Code Online (Sandbox Code Playgroud)

测试类:

package org.tim;

import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; …
Run Code Online (Sandbox Code Playgroud)

maven spring-boot junit5

6
推荐指数
1
解决办法
1891
查看次数

使用 ASP.NET Core 路由时的本地主机 404

每当我向控制器添加任何类型的路由时,每个请求都会以 404 结束。当没有 [Route] 时,应用程序可以正常工作,但当我添加它时,它就会中断。我下载的项目之前可以工作,在不同的机器上工作正常,而我的旧项目不再工作,所以可能有些东西被更新了/我破坏了一些东西。

ValuesController:

[Route("/api/[controller]")]
public class ValuesController : Controller
{
    private readonly ValuesService _valuesService;

    public ValuesController()
    {
        _valuesService = new ValuesService(); 
    }

    [HttpGet]
    IActionResult ReturnValues()
    {
        return Ok(_valuesService.ReturnValues());
    }
}
Run Code Online (Sandbox Code Playgroud)

启动:

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IStudentResearchGroupData, StudentResearchGroupData>();
    services.AddMvc();

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme =
                                   JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme =
                                   JwtBearerDefaults.AuthenticationScheme;
    }).AddJwtBearer(o =>
    {
        o.Authority = "http://localhost:59418";
        o.Audience = "researchgroups";
        o.RequireHttpsMetadata = false;
    });
}

// This method gets called by the runtime. Use this method to configure the HTTP …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-2.0 asp.net-core-routing

3
推荐指数
1
解决办法
5100
查看次数