Search code examples
javaspringhibernatejunit

Unit testing does not get attributes


I want to test (values that are definitely inside my database (I see these values when i am debugging)) I want to get get a list of strings from an linkedArrayList with a LinkedHashMap inside it (named names in code bellow).

I expect that this is wrong:

List<String> names = body.stream().map(e-> e.get("name")).collect(Collectors.toList());

Do you know how I can get my test passed by receiving a list of strings of of the attribute name?

DebugResults

public class FinanceRestControllerTests {
    private RestTemplate restTemplate;

    @Before
    public void setUp() {
        restTemplate = new RestTemplate();
    }
    
    @Test
    public void testGetContracts() {
        ResponseEntity<List> response = restTemplate.getForEntity("http://localhost:5050/finance/rest/contracts", List.class);
        List<Map<String,String>> body = response.getBody();
        System.out.println(body);
        MatcherAssert.assertThat(response.getStatusCodeValue(), Matchers.equalTo(200));
        List<String> names = body.stream().map(e-> e.get("name")).collect(Collectors.toList());
        names.forEach((n) -> System.out.println(n));
        MatcherAssert.assertThat(names, Matchers.containsInAnyOrder("Jen", "Nakzo", "Renes"));
    }
}

Solution

  • It looks like you have nested Maps. You can solve this by changing List<Map<String,String>> to List<Map<String, Object>>, then access the name key of the Map from the customer key:

    (String)((Map)e.get("customer")).get("name")