Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add inner classes to Spring application context for Unit Testing?

I have a bean whose business logic loads beans of a certain type from the ApplicationContext in order to process them.

For my jUnit tests, I would like to create some dummy beans within my unit test class and see if my bean under test properly processes them. However, I am not sure what the best way to accomplish this is.

If I just declare my inner class within my test class, Spring will not have it as part of its application context. I realize that I could inject my application context within my jUnit class, and then use appContext.registerPrototype() to add it, however, I thought there might be a cleaner way using annotations.

I've tried to annotate the internal class with @Component, but not surprisingly, it did not work.

public class PatchEngineTest extends TestBase {
    @Component
    protected class Patch1 extends PatchBaseImpl implements Patch{
        public void applyPatch() throws Exception {
            // does nothing
        }
    }


    @Autowired PatchRepository patchRepository;
    @Autowired Patch1 patch1;

    @Test
    public void test() {
        fail("Not yet implemented");
    }

}

Not surprisingly, I get the following error message:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ia.patch.PatchEngineTest$Patch1] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Is there any way of doing this?

like image 513
Eric B. Avatar asked Oct 18 '13 02:10

Eric B.


People also ask

How does JUnit test inner class methods?

First, we have to add a new inner class called A to our test class and annotate the inner class with the @Nested annotation. After we have created the A class, we have to add one setup, teardown, and test method to the created inner class.

How application context is loaded in Spring?

ApplicationContext is a corner stone of a Spring Boot application. It represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.

How can you access the application context in a Spring integration test?

By default the ApplicationContext is loaded using the GenericXmlContextLoader which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext by annotating fields in your test class with @Autowired , @Resource , or @Inject .

How can you create a shared application context in a JUnit integration test?

How can you create a shared application context in a JUnit integration test. Spring's integration testing support has the following primary goals: To manage Spring IoC container caching between tests. By default, once loaded, the configured ApplicationContext is reused for each test.


1 Answers

You need to make your inner class static; Spring can't instantiate a non-static inner class as a bean. If there really is a valid reason why it needs to be non-static, you could create it manually in an @Bean method.

like image 157
chrylis -cautiouslyoptimistic- Avatar answered Sep 21 '22 21:09

chrylis -cautiouslyoptimistic-