在单元测试中覆盖默认的Spring @Value注释值

时间:2018-02-04 13:47:40

标签: java spring unit-testing

我试图覆盖Spring @Value带注释的属性,该属性在测试类中具有默认值。

@Configuration
public class MyConfig {
    @Value("${MAX_CONN:200}")
    private int maxConn;

    //more code here
}

@RunWith(SpringRunner.class)
@ContextConfiguration(classes={MyConfig.class, PropertySourcesPlaceholderConfigurer.class}, loader=AnnotationConfigContextLoader.class)
@TestPropertySource(properties = {
        "MAX_CONN=2"
})
public class SomeTest {
    //tests here
}

我正在使用org.springframework.test.context.TestPropertySource注释(thanks for the advise)。在调试期间,我看到maxConn值仍然是200.如果从原始代码@Value("${MAX_CONN}")中删除了默认值,则maxConn值被覆盖为2。 还可以通过定义环境变量来覆盖默认属性。 我想知道是否有办法覆盖具有默认值的@Value注释属性?

注意:Spring版本 - 4.3.13

2 个答案:

答案 0 :(得分:1)

enter image description here

使用上述运行配置输出

MyConfig{maxConn=100}

Process finished with exit code 0

SpringBootWebApplication.java

package com.test;

import com.test.service.MyConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {

    @Autowired
    MyConfig myConfig;


    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(SpringBootConsoleApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
        //SpringApplication.run(SpringBootConsoleApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(myConfig);
    }
}

MyConfig.java

package com.test.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyConfig {

    @Value("${MAX.CONN:200}")
    private int maxConn;

    @Override
    public String toString() {
        return "MyConfig{" +
                "maxConn=" + maxConn +
                '}';
    }
}

TestProperties.java

import com.test.SpringBootConsoleApplication;
import com.test.service.MyConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootConsoleApplication.class)
public class TestProperties {

    static {
        System.setProperty("MAX.CONN", "2");
    }

    @Autowired
    MyConfig myConfig;

    @Test
    public void testSequence() {
        //System.out.println(myConfig);
        //...
    }

}

测试输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

MyConfig{maxConn=2}

Process finished with exit code 0

答案 1 :(得分:0)

对于在单元测试用例中使用属性,通常有两种方法,

1> 您应该拥有自己的一组用于测试的属性,文件应该在您的类路径下(/ src / test / resources)。

@PropertySource("classpath:application-test.properties ")

2 - ;另一个约定是在不同的类路径上具有相同名称的属性文件。根据您是否正在运行测试,您可以加载其中一个。

因此,在典型的应用程序中,它是:

src/test/resources/application.properties

src/main/resources/application.properties

并将其用作

@PropertySource("classpath:application.properties ")
相关问题