How to tell spring to only download the necessary beans for a JUnit test?

A simple question that may have an extended answer.

Question: My question is, is there a way to create an instance of only the classes in the context of your application needed for this particular JUnit test?

Reason: My application context is getting pretty big. I also do a lot of integration tests, so I think you will understand when I say that every time I run the test, all classes in my application context get an instance, and this takes time.

Example:

Tell the Foo Class Only String Entry

public class Foo { @Inject Bar bar; @Test public void testrunSomeMethod() throws RegisterFault { bar.runSomeMethod(); } 

but the application context has beans foobar and bar. I know this is not the context of the vaild application, but be sure all my code works.

 <beans> <bean id="foobar" class="some.package.FooBar"/> <bean id="bar" class="some.package.Bar"/> <beans> 

So, how do I tell spring to only instantiate the Bar and ignore the FooBar for the foo test class.

Thanks.

+6
source share
3 answers

This is not a direct answer, so I would not mark it as a solution. But I hope that it will be useful.

I usually see three options.

  • How well answered Vinayvulu. Create separate contexts and run them separately in each test.

  • Create a context once for all tests. Also here: Reusing the spring application context in junit test classes This is a great optimization for testing all tests at once.

  • Mix these two first points. Create one smaller context for testing purposes only. Sorry to never be tested, but may throw NPE, etc. Like here: Mockito Injection mocks spring bean to increase context build. And reuse it, as in step 2. Once build for all tests. Personally, I would go with that.

  • This one is waiting for an answer about some kind of smart test runner who creates the minimum necessary context for each test.

+2
source

Consider adding default-lazy-init = "true" to your spring xml beans tag (or add lazy-init = "true" to those specific beans that take a lot of time). This will ensure that only beans that are called using applicationContext.getBean (

(if you are using Java spring configuration, add @Lazy to the configuration files)

Caveat . If there is a bean that is not explicitly initialized with applicationContext.getBean () or is introduced as a dependency, the bean obtained with applicationContext.getBean (), then the bean will be no longer LONGER built or not initialized. Depending on your application, which may lead to malfunctions OR not. Perhaps you can highlight these beans as lazy-init = "false"

+4
source

Yes, we can do this using the context for a test case. Prepare a test context XML file with beans necessary for your test case.

If you are using maven, put test-context.xml in the src/test/resources folder.

Annotate your required test class with the following annotation

@ContextConfiguration(locations = "classpath:test-application-context.xml")

This helps to load only specific beans for a test case.

If you have two types of test cases, then

 @Runwith(SpringJUnit4Runner.class) @ContextConfiguration(locations = "classpath:test-context-case1.xml") public class TestClassCase1 {} @Runwith(SpringJUnit4Runner.class) @ContextConfiguration(locations = "classpath:test-context-case2.xml") public class TestClassCase2 {} 
+3
source

All Articles