【问题标题】:How to use JUnit TemporaryFolder @Rule with Spring @Value properties?如何将 JUnit TemporaryFolder @Rule 与 Spring @Value 属性一起使用?
【发布时间】:2020-03-01 22:46:39
【问题描述】:

我可以在junit 测试中生成TemporaryFolder,并将该文件夹用作@Value 属性的前缀吗?

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
    @Rule
    public TemporaryFolder tmp = new TemporaryFolder();


    @Autowired
    private MyService service;

    @Test
    public void test()  {
        //TODO how to rewrite the application property with the tmp folder created?
        service.run();
    }
}

@Service
public class MyService {
    @Value("${my.export.path}")
    private String path;

    public void run() {
        //generates a file and exports it to @Value path
    }
}

application.properties:

my.export.path=/var/www/export.csv

我当然想将导出路径设置为生成的 tmp 文件夹。但是怎么做呢?

【问题讨论】:

    标签: java spring spring-boot junit spring-boot-test


    【解决方案1】:

    一种可能的解决方案是通过 setter 将值注入 path(但这会强制更改被测类)或通过 Spring 的 ReflectionTestUtils 像这样:

    @Test
    public void test() {
      ReflectionTestUtils.setField(service, "path", tmp.getRoot().getAbsolutePath());
      service.run();
    }
    

    您还可以考虑创建一个内部配置类,该类为您提供具有所需路径的MyService bean。

    【讨论】:

      【解决方案2】:

      我可以通过@ClassRuleApplicationContextInitializer 重写ApplicationContext 中的属性来解决它。虽然它有效,但我可能仍然对更好的解决方案感兴趣!

      @RunWith(SpringRunner.class)
      @SpringBootTest
      @ContextConfiguration(initializers = TemporaryFolderInitializer.class)
      public class MyTest {
          @ClassRule
          public static TemporaryFolder tmp = new TemporaryFolder();
      
          public static class TemporaryFolderInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
              @Override
              public void initialize(ConfigurableApplicationContext applicationContext) {
                  TestPropertySourceUtils.addInlinedPropertiesToEnvironment(applicationContext, "my.export.path=" + tmp.getRoot().getAbsolutePath());
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 2019-10-10
        • 1970-01-01
        相关资源
        最近更新 更多