[英]Spring JUnit Test not loading full Application Context
Hi I am trying to so spring junit test cases... and I require my full application context to be loaded.嗨,我正在尝试使用 spring junit 测试用例...并且我需要加载我的完整应用程序上下文。 However the junit test does not initialize the full application context.但是,junit 测试不会初始化完整的应用程序上下文。
Test 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 Class 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 { ...
Hence it should scan all the spring bean in the package com.test and also load them into the applicationcontext for the Junit testcase.因此,它应该扫描 com.test 包中的所有 spring bean,并将它们加载到 Junit 测试用例的应用程序上下文中。 But from the output of the beans initalised it doesnt seem to be doing this.但是从 bean 初始化的输出来看,它似乎并没有这样做。
You need to annotate your test class with @ActiveProfiles
as follows;您需要使用@ActiveProfiles
注释您的测试类,如下所示; otherwise, your Application
configuration class will always be disabled .否则,您的Application
配置类将始终被禁用。 That's why you currently do not see any of your own beans listed in the ApplicationContext
.这就是为什么您目前在ApplicationContext
中看不到任何您自己的 bean 的原因。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ActiveProfiles(Constants.SPRING_PROFILE_DEVELOPMENT)
public class MongoDbRepositoryTest { /* ... */ }
In addition, Application
should be annotated with @Configuration
as was mentioned by someone else.此外, Application
应该像其他人提到的那样用@Configuration
进行注释。
您是否可能在Application
类中缺少@Configuration
注释?
在每个无法扩展的测试类中添加 @ActiveProfile 在 VM 选项中添加它
-Dspring.profiles.active=test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.