简体   繁体   English

Spring Test 无法获取 Spring Configuration Beans

[英]Spring Test cannot get Spring Configuration Beans

@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@ActiveProfiles(profiles = "test")
@RunWith(MockitoJUnitRunner.class)
public class ConfigurationTest {
    @Autowired
    private Environment environment;

    @Test
    public void test1() throws Exception {
        environment.getProperty("bhavya",Boolean.class);
    }

    @Configuration
    @Profile("test")
    @ComponentScan(basePackages={"com.bhavya.test"})
    public static class EnvironmentServiceTestConfiguration{

        @Bean
        @Primary
        public Environment environment(){
           return Mockito.mock(Environment.class);
        }
    }
}

I also tried putting EnvironmentServiceTestConfiguration as a non-inner non-static class, but didn't help.我也尝试将 EnvironmentServiceTestConfiguration 作为非内部非静态类,但没有帮助。

Here is what I tried in a separate class:这是我在单独的课程中尝试过的:

@RunWith(MockitoJUnitRunner.class)
@Profile("test")
@Configuration
class EnvironmentServiceTestConfiguration{

    @Bean
    @Primary
    public Environment environment(){
        return Mockito.mock(Environment.class)
    }
}

didn't work either也没有用

The test class is located in test/java/com.bhavya.test package.测试类位于 test/java/com.bhavya.test 包中。 I am trying to run this particular test test1我正在尝试运行这个特定的测试 test1

This is my first test of such kind.这是我第一次进行此类测试。 I have never before used AnnotationConfigContextLoader.class, enlighten me.以前没用过AnnotationConfigContextLoader.class,赐教。

Stacktrace :堆栈跟踪:

java.lang.NullPointerException java.lang.NullPointerException

at Line number where I have statement :在我有声明的行号:

environment.getProperty("bhavya",Boolean.class);

Try using @RunWith(SpringJUnit4ClassRunner.class)尝试使用@RunWith(SpringJUnit4ClassRunner.class)

Here is an example explaining how can be done https://www.mkyong.com/unittest/junit-spring-integration-example/这是一个解释如何完成的示例https://www.mkyong.com/unittest/junit-spring-integration-example/

Here is a code sample that works for the sample in the question:这是适用于问题中的示例的代码示例:

package com.example.demo.test;包 com.example.demo.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;

@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ConfigurationTest {
    @Autowired
    private Environment environment;

    @Test
    public void test1() throws Exception {
        System.out.println(environment.getProperty("bhavya",Boolean.class));
    }

    @Configuration
    @ComponentScan(basePackages={"com.example.demo.test"})
    public static class EnvironmentServiceTestConfiguration{

        @Bean
        @Primary
        public Environment environment(){
            Environment env =  Mockito.mock(Environment.class);
            when(env.getProperty(eq("bhavya"), eq(Boolean.class))).thenReturn(true);

            return env;
        }
    }
}

Try below annotations for your test class为您的测试类尝试以下注释

 @ContextConfiguration(classes = Config.class)
@SpringBootTest
@ExtendWith(SpringExtension.class)
@RunWith(SpringJUnit4ClassRunner.class)

    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM