【问题标题】:Set env variable in spock在 spock 中设置环境变量
【发布时间】:2021-12-20 03:04:11
【问题描述】:

我想为 fileDirName 设置环境变量,但不知道如何添加

在代码中:

@Value("${file.dir.name}")
private String fileDirName;

测试用例:

def 'test file'() {
    when:
        def result = service.getFiles()
    then:
        result.success == Boolean.TRUE
}

例外:

java.lang.IllegalArgumentException: file name must not be null.

如何在此处模拟环境变量

【问题讨论】:

    标签: java spring unit-testing spock


    【解决方案1】:

    java 进程一旦运行就不能修改env。但既然你是在测试弹簧,你就不必了。

    查看article 了解更多信息。最简单的就是使用@SpringBootTest

    @SpringBootTest(properties = { "file.dir.name=/tmp" })
    public class SpringBootPropertySourceResolverIntegrationTest {
    
        @Autowired
        MyService service
    
        def 'test file'() {
            when:
            def result = service.getFiles()
            then:
            result.success == Boolean.TRUE
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-13
      • 2011-03-25
      • 2015-02-01
      • 2017-01-01
      • 2017-10-30
      • 2012-03-22
      • 2012-06-30
      • 2015-04-06
      • 2019-11-22
      相关资源
      最近更新 更多