예제
@Getter
@NoArgsConstructor
class Model {
private int id;
private String name;
}
public class AssertJTest {
@Test
public void StringTest() {
Model model = new Model(10, "Jackson");
assertThat(model.getName()).isEqualTo("Jackson");
assertThat(model.getName()).startsWith("Ja")
.endsWith("on")
.isEqualToIgnoringCase("jackson");
}
@Test
public void ListTest() {
Model samuel = new Model(1, "Samuel");
Model jackson = new Model(2, "Jackson");
Model matt = new Model(3, "Matt");
Model nobody = new Model(4, "nobody");
List<Model> models = Stream.of(samuel, jackson, matt)
.collect(toList());
assertThat(models).hasSize(3)
.contains(samuel, matt, jackson)
.doesNotContain(nobody);
}
@Test
public void MapTest() {
Model samuel = new Model(1, "Samuel");
Model jackson = new Model(2, "Jackson");
Model matt = new Model(3, "Matt");
Model nobody = new Model(4, "nobody");
// when
List<Model> models = Stream.of(samuel, jackson, matt)
.collect(toList());
Map<Integer, String> modelMap = models.stream()
.peek(System.out::println) // debug
.collect(Collectors.toMap(Model::getId, Model::getName));
assertThat(modelMap).hasSize(3)
.contains(entry(1, "Samuel"), entry(2, "Jackson"), entry(3, "Matt"))
.doesNotContainEntry(4, "nobody");
}
}
Reference