개요

Assertion

class StudyTest {
		@Test
		@DisplayName("스터디 만들기 첫 번째")
		void create() throws Exception {
			Study study = new Study(10);
			assertAll(
				() -> assertNotNull(study),
				() -> assertEquals(StudyStatus.DRAFT, study.getStatus(),
						() -> "스터디를 처음 만들면 상태 값이 " + StudyStatus.DRAFT + " 상태다."),
				() -> assertTrue(study.getLimit() > 0, 
												"스터디 최대 참석 가능 인원은 0보다 커야 한다.")
			);
		}
	
		@Test
		@DisplayName("스터디 만들기 두 번째")
		void createTwo() throws Exception {
			IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, 
							() -> new Study(-10));
			String message = ex.getMessage();
			assertEquals("limit 은 0 보다 커야 한다.", message);
		}
	
		@Test
		@DisplayName("스터디 만들기 세 번째")
		void createThree() throws Exception {
			assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
				new Study(10);
				Thread.sleep(300);
			});
			// assertTimeoutPreemptively 는 Thread 를 사용한다.
			// Thread 공유가 안되는 ThreadLocal 를 사용하는 코드는 에러가 발생한다.
			// ThreadLocal 를 사용하는 코드는 assertTimeout 사용
		}
}

요약

실제 값이 기대한 값과 같은지 확인 assertEqulas(expected, actual)
값이 null이 아닌지 확인 assertNotNull(actual)
다음 조건이 참(true)인지 확인 assertTrue(boolean)
모든 확인 구문 확인 assertAll(executables...)
예외 발생 확인 assertThrows(expectedType, executable)
특정 시간 안에 실행이 완료되는지 확인 assertTimeout(duration, executable)
특정 시간 안에 실행이 완료되는지 확인
(지정한 timeout 도달하면 종료) assertTimeoutPreemptively(duration, executable)