How to exclude another @Controller from my context when testing with Spring Boot @WebMvcTest

I have several controllers, and I understand that by specifying one controller in @WebMvcTest, other controllers will not be loaded into the context. From docs

Controllers

- Specifies the controllers to check. May be left blank if all @Controller beans should be added to the application context.

My first 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); } } 

My other controller

 @Controller public class MyOtherController { @Autowired private MyOtherService myOtherService; etc... } 

My test for my 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()); } } 

But when I run this test, it does not try to load the MyOtherService bean from MyOtherContoller when loading the context.

 2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] osbfsDefaultListableBeanFactory : Creating shared instance of singleton bean 'myOtherController' 2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] osbfsDefaultListableBeanFactory : Creating instance of bean 'myOtherController' 2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] osbfannotation.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] osbfsDefaultListableBeanFactory : Eagerly caching bean 'myOtherController' to allow for resolving potential circular references 2017-09-28 11:50:11.687 DEBUG 16552 --- [ main] osbfannotation.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] osbfsDefaultListableBeanFactory : Creating shared instance of singleton bean 'myOtherService' 2017-09-28 11:50:11.688 DEBUG 16552 --- [ main] osbfsDefaultListableBeanFactory : Creating instance of bean 'myOtherService' 2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] osbfannotation.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] osbfannotation.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] osbfsDefaultListableBeanFactory : Eagerly caching bean 'myOtherService' to allow for resolving potential circular references 2017-09-28 11:50:11.689 DEBUG 16552 --- [ main] osbfannotation.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] oswcsGenericWebApplicationContext : 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)} 

I thought that the specification of the controller for testing in the WebMvcTest annotation would limit it to a single load of this controller. But his attempt to load another controller and because its beans are not mocking, it fails.

What am I missing or is my understanding wrong? I think I only needed to make fun of beans for the Controller under test. I also tried excludeFilter to specifically exclude the package for another controller, but this did not change the error.

+2
source share
2 answers

Please ensure that your Application.class test does not contain @ComponentScan annotations. For example, this is your package structure.

 abc-project +--pom.xml +--src +-- main +-- com +-- abc +-- Application.java +-- controller +-- MyController.java +-- test +-- com +-- abc +-- Application.java +-- controller +-- MyControllerTest.java 

Application.java in the test should look something like this,

 @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } 
+3
source

my.package.other.myOtherMapper (possibly a Mybatis Mapper file) is missing or is not explicitly initialized.

In my opinion, the myOtherService implementation class has a Mapper file that does not display correctly.

You may need to match them first. You can post the contents of the xml Mapper, if possible.

  <context:component-scan base-package="org.example"> <context:exclude-filter type="custom" expression="abc.xyz.MyOtherController"/> </context:component-scan> 
0
source

Source: https://habr.com/ru/post/1262437/


All Articles