application test : junit5 - assertion

This commit is contained in:
haerong22
2021-02-23 17:55:18 +09:00
parent e062e87a62
commit 181cef092f
3 changed files with 89 additions and 6 deletions

View File

@@ -1,4 +1,26 @@
package com.example.apptest;
public class Study {
private StudyStatus status;
private int limit;
public Study() {
}
public Study(int limit) {
if (limit <= 0) {
throw new IllegalArgumentException("limit은 0보다 커야 한다.");
}
this.limit = limit;
}
public StudyStatus getStatus() {
return this.status;
}
public int getLimit() {
return limit;
}
}

View File

@@ -0,0 +1,5 @@
package com.example.apptest;
public enum StudyStatus {
DRAFT, STARTED, ENDED
}

View File

@@ -2,13 +2,69 @@ package com.example.apptest;
import org.junit.jupiter.api.*;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*;
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class StudyTest {
@Test
@DisplayName("assertTimeout test")
void test_4() {
assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
Thread.sleep(300);
new Study(10);
});
// assertTimeout( 시간, 실행구문 )
// 실행 구문이 끝날 때 까지 기다렸다가 결과를 출력한다.
assertTimeout(Duration.ofMillis(100), () -> {
Thread.sleep(300);
new Study(10);
});
}
@Test
@DisplayName("assertThrows test")
void test_3() {
// 리턴 값은 해당 예외 타입이다.
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> new Study(-10));
// 에러 메시지 확인도 가능하다
assertEquals("limit은 0보다 커야 한다.", exception.getMessage());
}
@Test
@DisplayName("assertAll test")
void test_2() {
Study study = new Study(-10);
assertAll(
() -> assertTrue(study.getLimit() > 0, "스터디 최대 참석 가능 인원은 0보다 커야 한다."),
() -> assertEquals(StudyStatus.DRAFT, study.getStatus(), "스터디를 처음 만들면 status가 DRAFT 이어야 한다.")
);
}
@Test
@DisplayName("assertTrue test")
void test_1() {
Study study = new Study(10);
// assertTrue( 조건, 실패시 출력 메시지 )
assertTrue(study.getLimit() > 0, "스터디 최대 참석 가능 인원은 0보다 커야 한다.");
}
@Test
@DisplayName("assertEquals test")
void test() {
Study study = new Study();
// assertEquals( 기대값, 실제값, 실패시 메시지 )
assertEquals(StudyStatus.DRAFT, study.getStatus(), "스터디를 처음 만들면 status가 DRAFT 이어야 한다.");
}
@Test
void create_new_study() {
Study study = new Study();
assertNotNull(study);
@@ -21,12 +77,12 @@ class StudyTest {
System.out.println("create1 테스트");
}
// // @Disabled : 모든 테스트 실행 시 테스트 제외
// @Test
// @Disabled
// void create2() {
// System.out.println("create2 테스트");
// }
// @Disabled : 모든 테스트 실행 시 테스트 제외
@Test
@Disabled
void create2() {
System.out.println("create2 테스트");
}
//
// /**
// * 모든 테스트 실행 전에 한번 실행