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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28712134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:02:40  来源:igfitidea点击:

Spring JUnit Test not loading full Application Context

javaspringjunitspring-bootspring-test

提问by Shivam Sinha

Hi I am trying to so spring junit test cases... and I require my full application context to be loaded. However the junit test does not initialize the full application context.

嗨,我正在尝试使用 spring junit 测试用例......并且我需要加载我的完整应用程序上下文。但是,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. But from the output of the beans initalised it doesnt seem to be doing this.

因此,它应该扫描 com.test 包中的所有 spring bean,并将它们加载到 Junit 测试用例的应用程序上下文中。但是从 bean 初始化的输出来看,它似乎并没有这样做。

采纳答案by Sam Brannen

You need to annotate your test class with @ActiveProfilesas follows; otherwise, your Applicationconfiguration class will always be disabled. That's why you currently do not see any of your own beans listed in the ApplicationContext.

您需要使用以下注释对您的测试类进行注释@ActiveProfiles;否则,您的Application配置类将始终被禁用。这就是为什么您目前看不到任何您自己的 bean 列在ApplicationContext.

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

In addition, Applicationshould be annotated with @Configurationas was mentioned by someone else.

另外,Application应该@Configuration用其他人提到的注释。

回答by rikica

Are you maybe missing an @Configurationannotation on your Applicationclass?

您是否可能遗漏了类@Configuration上的注释Application