Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot NUnit 5 Spring MVC测试子模块中自动连线依赖项的NoSuchBean定义异常_Spring Boot_Spring Mvc_Jooq_Junit5 - Fatal编程技术网

Spring boot NUnit 5 Spring MVC测试子模块中自动连线依赖项的NoSuchBean定义异常

Spring boot NUnit 5 Spring MVC测试子模块中自动连线依赖项的NoSuchBean定义异常,spring-boot,spring-mvc,jooq,junit5,Spring Boot,Spring Mvc,Jooq,Junit5,我有一个有两个子模块的项目;一个是数据访问层,另一个是API服务。 数据访问模块在服务类中使用JOOQ和自连线DSLContext。另外,我正在使用JUnit5和SpringBoot2.2.4 数据访问模块中的QueryService类有一个成员,如@Autowired private dsl context dsl 测试类的设置如下所示: @SpringBootTest public class MyServiceTests { @Autowired QueryService ser

我有一个有两个子模块的项目;一个是数据访问层,另一个是API服务。 数据访问模块在服务类中使用JOOQ和自连线DSLContext。另外,我正在使用JUnit5和SpringBoot2.2.4

数据访问模块中的QueryService类有一个成员,如
@Autowired private dsl context dsl

测试类的设置如下所示:

@SpringBootTest
public class MyServiceTests {

  @Autowired
  QueryService service;

  @Autowired
  private DSLContext dsl;

  @Test
  public void TestDoSomething() throws Exception {
    service.selectBusinessEntityRelatedByBusinessEntity("C00001234", mockAuth);
  }
}
@WebMvcTest
public class CustomersControllerTests {

  @Autowired
  private MockMvc mockMvc;

  @Autowired
  private Gson gson;

  private static final String testSub = "329e6764-3809-4e47-ac48-a52881045787";

  @Test
  public void addCustomerTest() {

    var newCustomer = new Customer().firstName("John").lastName("Doe");
    mockMvc.perform(post("/customers").content(gson.toJson(newCustomer)).contentType(MediaType.APPLICATION_JSON)
        .with(jwt().jwt(jwt -> jwt.claim("sub", testSub)))).andExpect(status().isNotImplemented());
  }
此模块中的测试运行正常。配置从application.yaml读取,autowire将真实服务或模拟注入到我的QueryService和本地dsl中

API服务是另一回事。如果我在没有MVC的情况下使用@SpringBootTest注释,我可以成功地通过application.yaml中的配置将测试注入本地DSLContext。测试设置与此类似:

@SpringBootTest
public class CustomersControllerTests {

  @Autowired
  private Gson gson;

  @Autowired
  DSLContext dsl;

  @Test
  public void addCustomerTest() {
  }
不过,我需要使用@WebMvcTest,以便初始化MockMvc,但切换到@WebMvcTest会导致数据访问模块中实现的服务类中的注入失败。注入在查询服务类中找不到DSLContextbean。我这样设置测试:

@SpringBootTest
public class MyServiceTests {

  @Autowired
  QueryService service;

  @Autowired
  private DSLContext dsl;

  @Test
  public void TestDoSomething() throws Exception {
    service.selectBusinessEntityRelatedByBusinessEntity("C00001234", mockAuth);
  }
}
@WebMvcTest
public class CustomersControllerTests {

  @Autowired
  private MockMvc mockMvc;

  @Autowired
  private Gson gson;

  private static final String testSub = "329e6764-3809-4e47-ac48-a52881045787";

  @Test
  public void addCustomerTest() {

    var newCustomer = new Customer().firstName("John").lastName("Doe");
    mockMvc.perform(post("/customers").content(gson.toJson(newCustomer)).contentType(MediaType.APPLICATION_JSON)
        .with(jwt().jwt(jwt -> jwt.claim("sub", testSub)))).andExpect(status().isNotImplemented());
  }
这是实际错误:

2020-02-25 18:14:33.655  WARN 10776 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customersController': Unsatisfied dependency expressed through field '_customersService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customersService': Unsatisfied dependency expressed through field '_queryService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'queryService': Unsatisfied dependency expressed through field '_dsl'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.jooq.DSLContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
因此,我知道测试应用程序配置是正确的,因为它在不使用MVC注释时工作。此外,我可以在API项目测试中创建DSLContext,并且可以在测试之外实际运行API服务


那么,为什么在使用MVC测试设置时找不到DSLContext呢?

这可能是因为@WebMvcTest完全禁用了Spring Boot的自动配置,并且只扫描@Controllers和其他一些select类,这是您的..嗯..MVC测试所需要的

Spring文档建议在您的案例中这样做:

如果要加载完整的应用程序配置并使用MoCKMVC,则应考虑@ SpRunBooTebug与@ AutoCudioMoCKMVC而不是此注释。