Skip to content

Instantly share code, notes, and snippets.

@mhaligowski
Created June 2, 2017 04:43
Show Gist options
  • Save mhaligowski/a902ed35910b223633c0f187a0cd0947 to your computer and use it in GitHub Desktop.
Save mhaligowski/a902ed35910b223633c0f187a0cd0947 to your computer and use it in GitHub Desktop.
AssertJ collections example
public class Monster {
private String id;
private String name;
public Monster(final String id, final String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(final String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Monster monster = (Monster) o;
if (!id.equals(monster.id)) return false;
return name != null ? name.equals(monster.name) : monster.name == null;
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class MonsterTest {
private List<Monster> beasts;
private Monster direwolf;
private Monster werewolf;
private Monster vampire;
@Before
public void setUp() throws Exception {
direwolf = new Monster("id1", "Direwolf");
werewolf = new Monster("id2", "Werewolf");
vampire = new Monster("id3", "Vampire");
beasts = Lists.newArrayList(direwolf, werewolf);
}
@Test
public void should_contain_element() throws Exception {
assertThat(beasts).contains(direwolf);
assertThat(beasts).contains(werewolf);
assertThat(beasts).doesNotContain(vampire);
}
@Test
public void should_contain_all_the_elements_in_any_order() throws Exception {
assertThat(beasts).containsExactlyInAnyOrder(direwolf, werewolf);
assertThat(beasts).containsExactlyInAnyOrder(werewolf, direwolf);
}
@Test
public void should_contain_all_the_elements_in_order() throws Exception {
assertThat(beasts).containsExactly(direwolf, werewolf);
// this one is false
// assertThat(beasts).containsExactly(werewolf, direwolf);
}
@Test
public void should_contain_given_values_in_any_number() throws Exception {
List<Monster> group = Lists.newArrayList(direwolf, direwolf, werewolf, werewolf, werewolf);
assertThat(group).containsOnly(direwolf, werewolf);
assertThat(group).containsOnly(werewolf, direwolf);
}
@Test
public void should_contain_only_once() throws Exception {
List<Monster> group = Lists.newArrayList(direwolf, direwolf, werewolf, vampire);
assertThat(group).containsOnlyOnce(vampire);
assertThat(group).containsOnlyOnce(werewolf);
// this one is false
// assertThat(group).containsOnlyOnce(direwolf);
}
@Test
public void should_contain_the_properties() throws Exception {
assertThat(beasts).extracting(Monster::getName)
.containsExactly("Direwolf", "Werewolf");
}
@Test
public void should_contain_first_and_last_element_as_expected() throws Exception {
assertThat(beasts).startsWith(direwolf);
assertThat(beasts).endsWith(werewolf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment