ewok :
나는 유형의 개체 목록을 반환하는 방법을 MyClass
. MyClass
많은 특성을 가지고,하지만 난 걱정 type
하고 count
. 나는 반환 된 목록은 특정 조건과 일치하는 적어도 하나의 요소가 포함 주장 테스트를 작성하고자합니다. 예를 들어, 나는 유형의 목록에 적어도 하나 개의 원소 원하는 "Foo"
카운트로 1
.
말 그대로 반환 된리스트를 반복하고 내가 좋아하는, 통과 하나를 발견하면 파괴, 개별적으로 각각의 요소를 확인하지 않고이 작업을 수행하는 방법을 알아 내려고 노력 해요 :
boolean passes = false;
for (MyClass obj:objects){
if (obj.getName() == "Foo" && obj.getCount() == 1){
passes = true;
}
}
assertTrue(passes);
난 정말이 구조 좋아하지 않는다. 이 사용 할 수있는 더 좋은 방법이 있는지 궁금 해요 assertThat
일부 Matcher를이.
user2718281 :
hamcrest 수입과
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
당신과 함께 테스트 할 수 있습니다
assertThat(foos, hasItem(allOf(
hasProperty("name", is("foo")),
hasProperty("count", is(1))
)));