简体   繁体   English

Spring JUnit 测试未加载完整的应用程序上下文

[英]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.

相关问题 Spring boot junit 测试加载应用程序上下文失败 - Spring boot junit test failed loading application context junit / spring属性未加载应用程序上下文 - junit/spring properties not loading with application context 在JUnit测试中将Maven属性传递给Spring Application上下文文件 - Passing Maven properties to Spring Application context files in JUnit test Spring Batch JUnit测试未加载JobLauncherTestUtils的application.properties - Spring Batch JUnit test not loading application.properties for JobLauncherTestUtils "'application.yml' 中的 Spring Boot 属性未从 JUnit 测试加载" - Spring Boot properties in 'application.yml' not loading from JUnit Test JUnit测试启动Jetty,Jetty启动Spring,可以测试查看spring应用程序上下文 - JUnit test starts Jetty, Jetty starts Spring, can test see spring application context Spring 启动 controller 测试加载整个应用程序上下文 - Spring Boot controller test loading entire application context 使用新的 Spring Context 启动 JUnit 测试 - Start JUnit test with fresh Spring Context Junit 5,Spring Application Context在@DirtiesContext上未关闭 - Junit 5, Spring Application Context does not close on @DirtiesContext 捕获应用程序上下文错误spring junit - Catching application context error spring junit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM