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:

category-merchandiser:
-
username: test
password: blas3cret
roles:
- test
- admin


And this is my class:

@Configuration
@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:

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

@Autowired
CategoryMerchandiserUsersConfig config;

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


My console output tells me that the list is empty:

java.lang.AssertionError:
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