Spring JUnit Test does not load full application context

Hi, I am trying to use spring junit test cases ... and I need the full application context to download. However, the junit test does not initialize the full application context.

Testing Class:

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class MongoDbRepositoryTest { @Value("${spring.datasource.url}") private String databaseUrl; @Inject private ApplicationContext appContext; @Test public void testCRUD() { System.out.println("spring.datasource.url:" + databaseUrl); showBeansIntialised(); assertEquals(1, 1); } private void showBeansIntialised() { System.out.println("BEEEAAANSSSS"); for (String beanName : appContext.getBeanDefinitionNames()) { System.out.println(beanName); } } 

Output:

 spring.datasource.url:${spring.datasource.url} BEEEAAANSSSS org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.annotation.internalPersistenceAnnotationProcessor org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor 

Main application classes Annotations:

 @ComponentScan(basePackages = "com.test") @EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class }) @EnableMongoRepositories("com.test.repository.mongodb") @EnableJpaRepositories("com.test.repository.jpa") @Profile(Constants.SPRING_PROFILE_DEVELOPMENT) public class Application { ... 

Therefore, it must scan all the spring bean in the com.test package, and load them into the applicationcontext for the Junit test. But from the output of beans initialized, it does not seem to do this.

+6
source share
2 answers

You need to annotate your @ActiveProfiles test class as follows: otherwise your Application configuration class will always be disabled. This is why you currently do not see your own beans specified in the ApplicationContext .

 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @ActiveProfiles(Constants.SPRING_PROFILE_DEVELOPMENT) public class MongoDbRepositoryTest { /* ... */ } 

In addition, Application should be annotated using @Configuration , as mentioned by someone else.

+7
source

Perhaps you are missing the @Configuration annotation in your Application class?

0
source

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


All Articles