Issue importing properties in test

33 Views Asked by At

In a @RunWith(SpringRunner.class) test, I want to import some properties defined in com/.../test/resources/application.yml but they are not well formatted. This issue does not replicate when running the application.

Configuration class:

@ConfigurationProperties("my.property")
@Component
@Data
public class MyConfig{

    private List<String> val1;
    private List<String> val2;
}

main/.../application.yml:

my.property.val1: [string1, string2, string3]
my.property.val2: [string1, string2]

test/.../application.yml (just the same content):

my.property.val1: [string1, string2, string3]
my.property.val2: [string1, string2]

The test class:

@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application.yml")
@EnableConfigurationProperties(value = MyConfig.class)
public class MyTest {

    @Autowired
    private MyConfig myConfig;

When running the application, the object looks good (meaning that val1 is a list of string1, string2, string3), but when running the unit test, the [ and ] are also included. For example, val1 would be a list of the following Strings: [string1, string2, string3]

Basically, the issue is that the [ and ] characters are included (but this happens only on the testing part). Any ideas?

1

There are 1 best solutions below

0
John Williams On

Can you try setting the test values as the following. Normal structure for array in yaml:

my.property.val1:
  - string1
  - string2
  - string3
my.property.val2:
  - string1
  - string2