简体   繁体   English

Spring-boot - 参数化测试 - 访问MethodSource中的Application.properties

[英]Spring-boot - Parameterized Test - Access Application.properties in MethodSource

When writing a ParameterizedTest with spring-boot, one can specify a MethodSource which provides the values. 使用spring-boot编写ParameterizedTest时,可以指定提供值的MethodSource The MethodSource is a static method, which means accessing autowired values and members is not possible. MethodSource是一个静态方法,这意味着无法访问自动装配的值和成员。

I do define a value in the application.properties which is crucial for setting up the parameters(it points to the directory containing the data I need). 我在application.properties定义了一个值,这对于设置参数至关重要(它指向包含我需要的数据的目录)。 How can I access the value within the static method? 如何在静态方法中访问该值?

example code: 示例代码:

application.properties: application.properties:

com.example.directorypath=a/b/c

ApplicationTest: ApplicationTest:

@ActiveProfiles("dev")
@RunWith(SpringRunner.class)
@SpringBootTest
public class RdxApplicationTests {

    @Value("${com.example.directorypath}")
    private String directory;

    @ParameterizedTest
    @MethodSource("provideDirectories")
    public void test(File dir){
        System.out.println(dir);
    }

    private static Stream<Arguments> provideDirectories(){
        //here is the place I need the value
        File f = new File(directory);

        return Arrays.stream(Objects.requireNonNull(f.listFiles())).map(Arguments::of);

    }
}

A way to resolve the problem of static factory methods is the following: 解决静态工厂方法问题的方法如下:

Annotate the test class with @TestInstance(TestInstance.Lifecycle.PER_CLASS) which allows factory methods within the test class to not be static. 使用@TestInstance(TestInstance.Lifecycle.PER_CLASS)注释测试类,它允许测试类中的工厂方法不是静态的。

The annotation is a JUnit5 annotation. 注释是JUnit5注释。

Source: MethodSource 来源: MethodSource

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

相关问题 在 Junit 测试中覆盖默认的 Spring-Boot application.properties 设置 - Override default Spring-Boot application.properties settings in Junit Test 使用动态值覆盖 Junit 测试中的默认 Spring-Boot application.properties 设置 - Override default Spring-Boot application.properties settings in Junit Test with dynamic value Spring-Boot中的application.properties属性的UTF-8编码 - UTF-8 encoding of application.properties attributes in Spring-Boot 覆盖application.properties以在Spring-boot应用程序中进行集成测试 - Override application.properties for integration tests in spring-boot app 如何在 Spring-Boot 的生产过程中覆盖 application.properties? - How to override application.properties during production in Spring-Boot? spring-boot如何添加多个application.properties文件? - How to add multiple application.properties files in spring-boot? spring-boot application.properties中的环境变量错误 - Environment Variables in spring-boot application.properties error 春季启动application.properties问题-bug - spring-boot application.properties question -bug Spring-Boot&gt;使用JNDI设置application.properties - Spring-Boot > Setting application.properties using JNDI application.properties 中电子邮件数据中 Spring-Boot 中的环境变量 - environment variables in Spring-Boot in email data in application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM