How to use SpringApplicationConfiguration or ContextConfiguration to load small parts of an application

I am working on a spring-boot project, which is completely insignificant for me. I am currently using the @WebApplicationContext annotation to run any of my Junit tests, since I cannot get the application to load in any other way. My goal in asking this question is either to get a reportable answer on how to avoid using it, or links to projects using an applicable concept.

My specific goal: I would like to have a testing configuration that will not load the entire web application in order to test smaller services and subsets of classes.

Example: I currently have a series of 3 collectors. One for the parent and two others for the children

@Component public class ReportResponseAssembler { @Autowired private ParameterResponseAssembler parameterResponseAssembler; @Autowired private TimeRangeResponseAssembler timeRangeResponseAssembler; public ReportResponseAssembler makeResponse() { return new ReportResponseAssembler(); } } 

For testing purposes, I would like to load only these 3 classes and make them appropriately insert dependencies into the parent. Sort of:

 public class ReportResponseAssemblerTest { @Autowired ReportInstanceResponseAssembler reportResponseAssembler; @Test public void testPlaceHolder() { Assert.assertNotNull(reportResponseAssembler); } } 

I tried to do something as follows:

 @EnableAutoConfiguration @ComponentScan(basePackages = { "com.blahpackage.service.assembler" }) @Configuration public class TestContextConfiguration {} 

Then, feeding this to SpringApplicationConfiguration, but even when validating, it does not detect the applicable beans for automatic injection. Perhaps I need to designate them as @Beans directly in the configuration and return new instances? Are there any other good ways? Any links to sample projects or explanations you have will be great.

Thanks for answering so much for your time.

+5
source share
2 answers

What you are trying to do can be easily done with the following code:

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = TestContextConfiguration.class) public class ReportResponseAssemblerTest { @Autowired ReportInstanceResponseAssembler reportResponseAssembler; @Test public void testPlaceHolder() { Assert.assertNotNull(reportResponseAssembler); } } @EnableAutoConfiguration @ComponentScan(basePackages = { "com.blahpackage.service.assembler" }) @Configuration public class TestContextConfiguration { } 

The three classes you mentioned should be under com.blahpackage.service.assembler , and should also be annotated using Spring stereotype annotation, such as @Component or @Service . For example, you will have:

 @Component public class ReportResponseAssembler { @Autowired private ParameterResponseAssembler parameterResponseAssembler; @Autowired private TimeRangeResponseAssembler timeRangeResponseAssembler; public ReportResponseAssembler makeResponse() { return new ReportResponseAssembler(); } } @Component public class ParameterResponseAssembler { //whatever } 

However, I would advise you to use such a test rarely because of performance implications. I mean, if you have many of these types of tests, Spring needs to create and destroy a different application context for each of them, whereas if you use the same context and tests, Spring can (usually) cache the context. Check out this blog for more details.

+5
source

I would suggest not creating a test configuration at all. Your integration tests (I hope you know that unit test should not create a Spring context at all) will test a configuration that is not used during production.

I would suggest creating a Spring configuration for module / module / integration testing module. How can you import these configurations into other contexts using the @Import annotation.

There is a huge advantage to the batch approach that you can specify a private package (with default access modifier) ​​beans

 @Component class SomeBeanClass{ } 

They can only be disabled using beans from the same package. This is a convenient way to encapsulate Spring beans.

Such granular Spring configurations can be easily tested with the integration test in isolation.

+2
source

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


All Articles