我有多个 Controller ,我的理解是,通过在@WebMvcTest 中指定一个 Controller ,其他 Controller 将不会加载到上下文中。来自docs
controllers - Specifies the controllers to test. May be left blank if all @Controller beans should be added to the application context.
我的第一个 Controller
@Controller
public class MyController {
@Autowired
private MyService myService;
private final Logger logger = Logger.getLogger(this.getClass());
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<String> index() {
try {
myService.get();
return new ResponseEntity<String>(HttpStatus.OK);
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
return new ResponseEntity<String>("REQUEST FAILED", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
我的另一个 Controller
@Controller
public class MyOtherController {
@Autowired
private MyOtherService myOtherService;
etc...
}
我对我的 Controller 的测试
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = { MyController.class }, secure = false)
@ActiveProfiles({ "test" })
public class MyControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
MyService myService;
@Test
public void testBaseReq() throws Exception {
Testing dummyData = new Testing();
dummyData.setData("testing");
when(myService.get(anyInt())).thenReturn(dummyData);
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk());
}
}
但是当我运行这个测试时,它在加载上下文时尝试从 MyOtherContoller 加载 bean MyOtherService 失败。
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'myOtherController'
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'myOtherController'
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [my.package.other.myOtherController]: AutowiredFieldElement for private my.package.other.myOtherService my.package.other.myOtherController.myOtherService
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'myOtherController' to allow for resolving potential circular references
2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Processing injected element of bean 'myOtherController': AutowiredFieldElement for private my.package.other.myOtherService my.package.other.myOtherController.myOtherService
2017-09-28 11:50:11.688 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'myOtherService'
2017-09-28 11:50:11.688 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'myOtherService'
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [my.package.other.myOtherService]: AutowiredFieldElement for private my.package.other.myOtherMapper my.package.other.myOtherService.myOtherMapper
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Registered injected element on class [my.package.other.myOtherService]: AutowiredFieldElement for private ie.aib.services.coredemo.FinancialsRegionService my.package.other.myOtherService.financialsRegionService
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Eagerly caching bean 'myOtherService' to allow for resolving potential circular references
2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] o.s.b.f.annotation.InjectionMetadata : Processing injected element of bean 'myOtherService': AutowiredFieldElement for private my.package.other.myOtherMapper my.package.other.myOtherService.myOtherMapper
2017-09-28 11:50:11.690 WARN 16552 --- [ 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 'myOtherController': Unsatisfied dependency expressed through field 'myOtherService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myOtherService': Unsatisfied dependency expressed through field 'myOtherMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'my.package.other.myOtherMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我认为在 WebMvcTest 注释中指定要测试的 Controller 会将其限制为仅加载该 Controller 。但是它试图加载另一个 Controller ,并且因为它的 bean 没有被模拟,所以它失败了。
我错过了什么或者我的理解不正确?我想我应该只需要为被测 Controller 模拟 bean。我还尝试了一个 excludeFilter 来专门排除另一个 Controller 的包,但这并没有改变错误。
最佳答案
请确保您的测试选择的 Application.class 不包含 @ComponentScan 注释。例如,这是你的包结构
abc-project
+--pom.xml
+--src
+-- main
+-- com
+-- abc
+-- Application.java
+-- controller
+-- MyController.java
+-- test
+-- com
+-- abc
+-- Application.java
+-- controller
+-- MyControllerTest.java
test 下的 Application.java 应该类似于这个例子,
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
关于java - 使用 Spring Boot @WebMvcTest 进行测试时如何从我的上下文中排除其他 @Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46460131/