Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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启动-参数化测试-访问MethodSource中的Application.properties_Java_Spring Boot_Junit - Fatal编程技术网

Java Spring启动-参数化测试-访问MethodSource中的Application.properties

Java Spring启动-参数化测试-访问MethodSource中的Application.properties,java,spring-boot,junit,Java,Spring Boot,Junit,使用spring boot编写ParameterizedTest时,可以指定提供值的MethodSource。MethodSource是一种静态方法,这意味着无法访问自动连接的值和成员 我确实在application.properties中定义了一个值,这对于设置参数至关重要(它指向包含我需要的数据的目录)。如何访问静态方法中的值 示例代码: application.properties: com.example.directorypath=a/b/c 应用程序测试: @ActiveProfi

使用spring boot编写
ParameterizedTest
时,可以指定提供值的
MethodSource
MethodSource
是一种静态方法,这意味着无法访问自动连接的值和成员

我确实在
application.properties
中定义了一个值,这对于设置参数至关重要(它指向包含我需要的数据的目录)。如何访问静态方法中的值

示例代码:

application.properties:

com.example.directorypath=a/b/c
应用程序测试:

@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);

    }
}
@ActiveProfiles(“dev”)
@RunWith(SpringRunner.class)
@春靴测试
公共类RDX应用程序测试{
@值(${com.example.directorypath}”)
私有字符串目录;
@参数化测试
@MethodSource(“提供的目录”)
公共无效测试(文件目录){
系统输出打印项次(dir);
}
私有静态流提供的目录(){
//这是我需要价值的地方
文件f=新文件(目录);
返回Arrays.stream(Objects.requirennull(f.listFiles()).map(Arguments::of);
}
}

解决静态工厂方法问题的方法如下:

@TestInstance(TestInstance.Lifecycle.PER_class)
注释测试类,这允许测试类中的工厂方法不是静态的

该注释是JUnit5注释

资料来源: