assertEquals(예상 값, 실제 값, 에러메세지)
package junit;
public class Study {
private StudyStatus status = StudyStatus.DRAFT;
public StudyStatus getStatus() {
// TODO Auto-generated method stub
return this.status;
}
}
package junit;
public enum StudyStatus {
DRAFT, STARTED, ENDED;
}
class StudyTest {
@Test
@DisplayName("스터디 만들기")
void create_new_study() {
Study study = new Study();
assertNotNull(study);
assertEquals(StudyStatus.DRAFT, study.getStatus(), ()->"Study 처음 값은 DRAFT여야 한다.");
}
package junit;
public class Study {
private StudyStatus status;
private int limit;
public Study(int limit) {
this.limit = limit;
}
public StudyStatus getStatus() {
// TODO Auto-generated method stub
return this.status;
}
public int getLimit() {
// TODO Auto-generated method stub
return this.limit;
}
}
package junit;
public enum StudyStatus {
DRAFT, STARTED, ENDED;
}
class StudyTest {
@Test
@DisplayName("스터디 만들기")
void create_new_study() {
Study study = new Study(-10);
assertNotNull(study);
assertEquals(StudyStatus.DRAFT, study.getStatus(), ()->"Study 처음 값은 DRAFT여야 한다.");
assertTrue(study.getLimit()>0, ()->"스터디 최대 참석 가능 인원은 0보다 커야한다.");
}
이때 assertTrue도 실패해야 하지만 앞선 assertEquals가 실패라서
assertTrue의 실패 여부를 알려주지 않는다.
class StudyTest {
@Test
@DisplayName("스터디 만들기")
void create_new_study() {
Study study = new Study(-10);
assertAll(
()->assertNotNull(study),
()->assertEquals(StudyStatus.DRAFT, study.getStatus(),
()->"Study 처음 값은 DRAFT여야 한다."),
()->assertTrue(study.getLimit()>0, ()->"스터디 최대 참석 가능 인원은 0보다 커야한다.")
);
}
}
package junit;
public class Study {
private StudyStatus status;
private int limit;
public Study(int limit) {
if(limit<0) {
throw new IllegalArgumentException("limit은 보다 커야한다.");
}
this.limit = limit;
}
public StudyStatus getStatus() {
// TODO Auto-generated method stub
return this.status;
}
public int getLimit() {
// TODO Auto-generated method stub
return this.limit;
}
}
class StudyTest {
@Test
@DisplayName("스터디 만들기")
void create_new_study() {
assertThrows(IllegalArgumentException.class, () -> new Study(-10));
}
}
예상한 예외 값이므로 테스트는 성공이다.
class StudyTest {
@Test
@DisplayName("스터디 만들기")
void create_new_study() {
assertTimeout(Duration.ofMillis(100), () -> {
new Study(10);
Thread.sleep(300);
});
}
}
예상 시간보다 타임아웃이기 때문에 테시트는 실패이다.