DevRock DevRock - 3 years ago 210
YAML Question

Testing reading list from yaml file into list object

I'm trying to load a list from application-test.yaml into the class, but it doesn't work.

This is how my application-test.yaml looks like:

username: test
password: blas3cret
- test
- admin

And this is my class:

@ConfigurationProperties(prefix = "category-merchandiser")
public class CategoryMerchandiserUsersConfig {

private List<CategoryMerchandiser> users = new ArrayList<>();
** getters & setters **

public static class CategoryMerchandiser {

private String username;
private String password;
private List<String> roles = new ArrayList<>();

** getters & setters **

This is how i'm trying to test if the size of my list is 2:

@ContextConfiguration(classes = CategoryMerchandiserUsersConfig.class)
public class CategoryMerchandiserUsersTest {

CategoryMerchandiserUsersConfig config;

public void shouldLoadApplicationYamlToClass() {
assertThat(config.getUsers().size(), is(2));

My console output tells me that the list is empty:

Expected: is <2>
but: was <0>
Expected :is <2>

Actual :<0>

What am i doing wrong? Hope you guys can help me.

Answer Source

It works! My problem was that i've no application context. So i moved the tests into integration test directory. That's all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download