테스트 경로 고려 : 비즈니스 로직을 보고 테스트 케이스에 영향을 줄 수 있는 조건과 그 조합을 찾아야 한다.
- Criteria 인스턴스가Criterion 객체를 포함하지 않을 때(27행)
- Criteria 인스턴스가 다수의Criterion 객체를 포함할 때(27행)
- answers.get()에서 반환된Answer 객체가null일 때(29행)
- criterion.getAnswer() 혹은 criterion.getAnswer().getQuestion Text()의 반환값이 null일 때(29행)
- criterion.getWeight()의 반환값이 Weight.DontCare여서match 변수가 true일 때(30행)
- value 변수와 criterion.getWeight()가 매칭되어 match 변수가 true일 때(30행)
- 두 조건문이 모두 false여서 결과적으로 match 변수가 false가 될 때(30행)
- match 변수가 false이고 criterion.getWeight()가 Weight.MustMatch여서 kill 변수가 true일 때(34행)
- match 변수가 true이기 때문에 kill 변수가 변하지 않을 때(34행)
- criterion.getWeight()가 Weight.MustMatch가 아니기 때문에 kill 변수가 변하지 않을 때(34행)
- match 변수가 true이기 때문에 score 변수가 업데이트되었을 때(37행)
- match 변수가 false이기 때문에 score 변수가 업데이트되지 않았을 때(37행)
- kill 변수가 true이기 때문에 matches 메서드가 false를 반환할 때(42행)
- kill 변수가 false이고 anyMatches 변수가 true이기 때문에 matches 메서드가 true를 반환할 때(42행과 44행)
- kill 변수가 false이고 anyMatches 변수가 false이기 때문에 matches 메서드가 false를 반환할 때(42행과 44행)
테스트 클래스에는 static 필드를 피해라 : static 필드를 사용하는 클래스들은 독립적이지 않고 상호 의존성이 존재한다. static 필드를 사용하는 클래스는 다른 테스트에 영향을 주기 때문에, 실패 원인 파악에 엄청난 노력이 든다.
Junit @Before : 모든 테스트 이전에 실행되어야하는 초기화 작업 수행
하나만 선택하는 것이 좋다
assertTrue(account.hasPositiveBalance());
assertTrue(account.getBalance() > initialBalance);
assertThat(account.getBalance(), equalTo(100));
assertThat(account.getBalance() > 0, is(true));
assertThat(new String[] {"a", "b"}, equalTo(new String[] {"a", "b"}));
assertTrue(account.getName().startsWith("xyz"));
// 여러 조건
assertThat(account.getName(), allOf(startsWith("my"), endsWith("acct")));
assertThat(account.getName(), anyOf(startsWith("my"), endsWith("loot")));
assertThat(account.getName(), not(equalTo("plunderings")));
// null 체크
assertThat(account.getName(), is(not(nullValue())));
assertThat(account.getName(), is(notNullValue()));