Search code examples
springspring-bootjunit5

How to properly use the @SelectFile annotation in a Unit 5 test class?


I am writing unit tests for a java class where I need to pass a file as a parameter. I recently came across the @SelectFile annotation in Spring Boot, which seems like a good solution for my use case. However, I am not sure how to use it properly. I am using junit 5 and Spring Boot framework.

Here's an example of what I'm trying to do:

@SelectFile(value = "/file.json")
class DataFormatTest {
    @Test
    void formatTest() throws Exception {
        Format format = new Format();
        //format.formatFile(file.json);
    }
}

Solution

  • You should use @ParametrizedTest and @MethodSource for your purpose. For ex

    class SomeTest {
    
        public static Stream<Arguments> getFiles() {
            return Stream.of(Arguments.arguments(new File("/file.json")),
                    Arguments.arguments(new File("/file2.json")));
        }
    
        @ParameterizedTest
        @MethodSource("getFiles")
        void doTest(File file) {
            assertFalse(file.exists());
        }
    }
    

    @SelectFile annotation is not for your issue.