0

I want to set the env variable for fileDirName but not sure how to add

In code:

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

Test case:

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

Exception:

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

How can I mock the env variable here

raaeusagvc
  • 71
  • 6

1 Answers1

1

You cannot modify the env of a java process once it is running. But since you are testing spring you don't have to.

Take a look at this article for more information. The easiest is to just use @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
    }
}
Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66