| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Spring 2.2 이상 버전의 SpringBoot 프로젝트 생성시 기본적으로 JUnit5 의존성이 포함된다.
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.2'default shortcut 기준으로 command + N으로 constructor, getter/setter 등을 만들 수 있는데, 이 때 test class도 생성할 수 있다.
ctrl + shift + R
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class AppTest {
@Test
void create_new_app() {
App app = new App();
assertNotNull(app);
}
}위의 경우에는 해당 테스트명이 create new app으로 표시되게 된다.
@DisplayNameGeneration(DisplayNameGenerator.IndicativeSentences)
class AppTest {
@ParameterizedTest(name = "Name is {0}")
@ValueSource(ints = {1, 2, 3})
void testFunc() {
// ...
}
}class AppTest {
@Test
@DisplayName("assertThrows 작성")
void checkThrows() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> new App(-10));
String message = exception.getMessage();
assertEquals("limit은 0보다 커야 한다.", message);
}
}class AppTest {
@Test
void create_app() {
App app = new App();
app.setStatus(AppStatus.DRAFT);
app.setLimit(-10);
/*
assertNotNull(app);
assertEquals(AppStatus.DRAFT, app.getStatus(),
() -> "스터디를 처음 만들면 상태값이 " + AppStatus.DRAFT + "여야 한다."); // 통과
assertTrue(app.getLimit() > 0, "스터디 최대 참석 가능 인원은 0보다 커야 한다."); // 실패
*/
// 위에 주석처리한 코드는 순서대로 테스트가 이루어졌다면, 테스트들을 모두 동시에 실행하는 메서드가 assertAll
assertAll(
() -> assertNotNull(app),
() -> assertEquals(AppStatus.DRAFT, app.getStatus(),
() ->"스터디를 처음 만들면 상태값이 " + AppStatus.DRAFT + "여야 한다."),
() -> assertTrue(app.getLimit() > 0, "스터디 최대 참석 가능 인원은 0보다 커야 한다.")
);
}
}테스트 실패시 콘솔에 해당 메세지가 출력되게 할 수 있다. 이 메세지를 람다식 형태로 전달해주면 필요할 때만 실행시키게 된다. (최소한으로 실행하여 리소스를 아낄 수 있다.)
class AppTest {
@Test
void check_equal() {
App app = new App();
app.setStatus(AppStatus.STARTED);
assertEquals(AppStatus.DRAFT, app.getStatus(), () -> "스터디를 처음 만들면 상태값이 " + AppStatus.DRAFT + "여야 한다.");
}
}| Back | FazBrowse Home | New Git URL |