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

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


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.


I also met this problem, and below is my solution :

@RunWith(SpringRunner.class)@SpringBootTest@ComponentScan@ImportAutoConfigurationpublic class FooTest {    @Component    public static class Bar {    }}

I used spring boot 2.1.4.RELEASE