Model, Model를 요소로 두는 Array, Map, List를 쉽게 테스트할 수 있는 AssertJ 테스트 코드이다.


import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.StrictAssertions.entry;


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");
}
}

class Model {
private int id;
private String name;

public Model(int id, String name) {
this.name = name;
this.id = id;
}

public String getName() {
return name;
}

public int getId() {
return id;
}

public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}

}


Posted by '김용환'
,