问题描述
我有一个包含两个子模块的项目;一个是数据访问层,另一个是API服务.数据访问模块在服务类中使用JOOQ和自动连接的DSLContext.另外,我正在使用JUnit 5和Spring Boot 2.2.4.
I have a project with two submodules; one is the data access layer the other is the API service.The data access module uses JOOQ and an autowired DSLContext in a service class. Also, I'm using JUnit 5, and Spring Boot 2.2.4.
数据访问模块中的QueryService类具有类似@Autowired private DSLContext dsl
;
The QueryService class in the data access module has a member like @Autowired private DSLContext dsl
;
测试类的设置如下:
@SpringBootTest
public class MyServiceTests {
@Autowired
QueryService service;
@Autowired
private DSLContext dsl;
@Test
public void TestDoSomething() throws Exception {
service.selectBusinessEntityRelatedByBusinessEntity("C00001234", mockAuth);
}
}
此模块中的测试正常运行.从application.yaml中读取配置,然后自动装配将真实服务或模拟注入到我的QueryService和本地dsl中.
The tests in this module run correctly. Configuration is read from the application.yaml, and autowire injects either real services or a mock into both my QueryService and the local dsl.
API服务是另一回事.如果我在不使用MVC的情况下使用@SpringBootTest批注,则可以成功获取测试,以通过application.yaml中的配置注入本地DSLContext.测试设置与此类似:
The API service is a different story. If I use the @SpringBootTest annotation with no MVC I can successfully get the tests to inject a local DSLContext with configuration from the application.yaml. Test set up similar to this:
@SpringBootTest
public class CustomersControllerTests {
@Autowired
private Gson gson;
@Autowired
DSLContext dsl;
@Test
public void addCustomerTest() {
}
尽管我需要使用@WebMvcTest,以便初始化MockMvc,但是切换到@WebMvcTest会导致在数据访问模块中实现的服务类中注入失败.注入无法在查询服务类中找到DSLContext bean.我像这样设置测试:
What I need though is to use @WebMvcTest so that MockMvc is initialized but switching to @WebMvcTest causes injection to fail in the service class implemented in the data access module. The injection fails to find the DSLContext bean within the query service class. I set up the test like this:
@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服务.
So, I know the test application configuration is correct because it works when not using MVC annotation. Also, I can create a DSLContext in the API project tests and I can actually run the API service outside the test.
那么,为什么在使用MVC测试设置时找不到DSLContext?
So, why cant the DSLContext be found when using the MVC test setup?
推荐答案
这可能是因为@WebMvcTest完全禁用了Spring Boot的自动配置,并且只扫描了.Controller所需的@Controllers和其他一些选择类. ..MVC测试..
This might be because @WebMvcTest fully disables Spring Boot's Autoconfiguration and only scans in @Controllers and a few other select classes, that you need for your ..well...MVC tests..
Spring文档建议针对您的情况执行此操作:
The Spring documentation recommends doing this in your case:
这篇关于NUnit 5 Spring MVC测试NoSuchBeanDefinitionException用于子模块中的自动关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!