Search code examples
javaspringspring-bootjunit4

SpringBoot JUnit and @Value


In my configuration class I have

@Value("${some.vars}")
private List<String> vars;

Now in my test I want to be able to set the value of this so I have this

@SpringBootTest
public class MyTest {

 @Test
 public void test() {
   ApplicationContextRunner runner = new ApplicationContextRunner();
   runner
   .withConfiguration(AutoConfigurations.of(MyConfiguration.class))
   .withUserConfiguration(UserConfiguration.class)
   .withPropertyValues("some.vars=A,B,C")
   .run(ctx -> {
     // some test assertions
   }); 
 }

I am getting A,B,C as a one string binded to List<String> in its 0th position. I expect it to render and bind as List in vars


Solution

  • You need extra work to split string as list

    @Value("#{'${some.vars}'.split(',')}") 
    private List<String> vars;