0

I want to check a list that is inside a Map value in a unit test using the AssertJ library:

public class Group {
    List<Player> players = new ArrayList<>();

    public Group(List<Player> players) {
        this.players.addAll(players);
    }
}
Map<Character, Group> generatedGroups = receiveFromAnyMethod();

Inside a Map I have this:

A: Group A -> players(playerA, playerB)

How I check a list inside a Group? I think I should use a extracting, flatExtracting methods, but I don't know how.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • What do you mean “check a group”? – Basil Bourque Nov 04 '21 at 14:20
  • I mean get a list inside a group and check you size, for example. I need somehow to access the list inside a "Group" and working with it. – Kauan Mocelin Nov 04 '21 at 14:29
  • 1
    Do you know already which element of the map contains the group (and the inner list) you want to test, or do you want to check that at least one entry in the map has a list that satisfies your checks? – Stefano Cordio Nov 04 '21 at 21:22
  • @StefanoCordio I already know which element will be taken. For example: I'll get a entry map passing 'A' key and then access the players list inside a "Group". – Kauan Mocelin Nov 05 '21 at 11:25

3 Answers3

2

Assuming that character is the input key, and player1 and player2 are the expected content of the inner list, you can write:

assertThat(generatedGroups.get(character))
  .extracting("players", as(InstanceOfAssertFactories.LIST))
  .containsExactly(player1, player2);

or with type safety, in case Group offers a getPlayers():

assertThat(generatedGroups.get(character))
  .extracting(Group::getPlayers, as(InstanceOfAssertFactories.LIST))
  .containsExactly(player1, player2);

Reference:

Stefano Cordio
  • 1,687
  • 10
  • 20
0
for (Map.Entry<Character, Group> entry : generatedGroups.entrySet()) {
    
    List<Player> players = entry.getValue().getPlayers();
    // Do your check here
}
stephane brun
  • 234
  • 1
  • 10
  • My question was edited and removed from title "AssertJ". I want to do this using AssertJ library. – Kauan Mocelin Nov 04 '21 at 14:35
  • anyway, i showed you how to access the list inside your map. Now do all the checks you want. If you don't know how to use AssertJ I'm sure there are plenty of exemples all around the web. Like : https://stackoverflow.com/questions/48042592/asserting-properties-on-list-elements-with-assertj – stephane brun Nov 04 '21 at 14:46
  • My question is only about using AssertJ library, and I already searched about this topic and founded not about it. Maybe I didn't make clear that this question is about testing, sorry. – Kauan Mocelin Nov 04 '21 at 15:02
  • 1
    @KauanMocelin That edit was done (by me) because including tags in the title like you did should not be done. That is what the tag system is for. You didn't mention AssertJ explicitly in the body of the question, so I have edited your question to do so. – Mark Rotteveel Nov 04 '21 at 16:57
0

Below is slight different answer with specific use case.

We wanted to assert the existence of header in the Response object. Below is the unit test using AssertJ assertions to verify the headers ResponseEntity.ok().contentType(APPLICATION_JSON)

assertThat(response).hasFieldOrPropertyWithValue("statusCode", OK)
          .extracting("headers")
          .isInstanceOf(HttpHeaders.class)
          .hasFieldOrPropertyWithValue("Content-Type",List.of(APPLICATION_JSON_VALUE));
Sanjay Bharwani
  • 3,317
  • 34
  • 31