Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重写单元测试中的默认Spring@Value注释值_Java_Spring_Unit Testing - Fatal编程技术网

Java 重写单元测试中的默认Spring@Value注释值

Java 重写单元测试中的默认Spring@Value注释值,java,spring,unit-testing,Java,Spring,Unit Testing,我试图重写测试类中具有默认值的Spring@Value注释属性。 我正在使用org.springframework.test.context.TestPropertySource注释来实现此目的()。在调试期间,我看到maxConn值仍然是200。如果从原始代码@value(${MAX\u CONN}”)中删除默认值,则maxConn值将被2覆盖。 也可以通过定义环境变量来覆盖默认属性。 我想知道是否有办法覆盖带有默认值的@Value注释属性 注:Spring版本-4.3.13 具有以上运行配

我试图重写测试类中具有默认值的Spring@Value注释属性。

我正在使用
org.springframework.test.context.TestPropertySource
注释来实现此目的()。在调试期间,我看到maxConn值仍然是200。如果从原始代码
@value(${MAX\u CONN}”)
中删除默认值,则maxConn值将被2覆盖。 也可以通过定义环境变量来覆盖默认属性。 我想知道是否有办法覆盖带有默认值的@Value注释属性

注:Spring版本-4.3.13

具有以上运行配置的输出

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> 您应该有自己的一组用于测试的属性,并且文件应该位于类路径(/src/test/resources)下

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

因此,在典型布置的应用中,它是:

src/test/resources/application.properties

并将其用作

@PropertySource("classpath:application.properties ")

您可以使用
ReflectionTestUtils.setField()。我只是想知道是否有更优雅的方法。对于
单元测试
您不必初始化Spring,以防您不测试smth。特定于Spring:如数据库工作或MVC.@oleg.cherednik-最后,使用Mockito和ReflectionTestUtils实现“无弹簧”单元测试。更多的代码,更少的时间,不是吗voodoos@anuta但是它在大型项目中节省了很多时间,Spring团队在他们的文档中推荐了这种方法。我在SpringBoot1.5.1和SpringFramework 4.3.6中尝试过这种方法。让我用Spring boot 1.5.9做同样的尝试,它将使用Spring Framework 4.3.13我们不使用属性文件,而是使用环境variables@anuta我已使用环境变量更新了答案。问题尚未解决,因为我需要在单元测试本身中以编程方式重写属性。我决定不在问题注释中提到的单元测试中使用spring。@anuta如果您在测试使用spring注释的代码时没有使用spring,那么从概念上讲,代码不会进行全面测试。是的,您可以使用powermock和mockito进行测试,但不适合这种情况。
src/test/resources/application.properties
src/main/resources/application.properties
@PropertySource("classpath:application.properties ")