From fabf82ba5529cf0eb491b1adfe9e499664f6a959 Mon Sep 17 00:00:00 2001 From: chaerlo127 Date: Fri, 27 Jan 2023 01:20:27 +0900 Subject: [PATCH] =?UTF-8?q?chap=205:=20JUnit=20Test=20Method=20=EA=B3=B5?= =?UTF-8?q?=EB=B6=80=20#47?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/java/testdriven/chap05/BadTest.java" | 25 +++++++++ .../testdriven/chap05/DisplayNameTest.java" | 26 +++++++++ .../testdriven/chap05/LifecycleTest.java" | 33 ++++++++++++ .../java/testdriven/chap05/TestMethod.java" | 53 +++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 "\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/BadTest.java" create mode 100644 "\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/DisplayNameTest.java" create mode 100644 "\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/LifecycleTest.java" create mode 100644 "\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/TestMethod.java" diff --git "a/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/BadTest.java" "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/BadTest.java" new file mode 100644 index 0000000..98589bb --- /dev/null +++ "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/BadTest.java" @@ -0,0 +1,25 @@ +package testdriven.chap05; + +import org.junit.jupiter.api.Test; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class BadTest { +// private FileOperator op = new FileOperator(); +// private static File file; +// +// @Test +// void fileCreationTest(){ +// File createdFile = op.createFile(); // 파일 생성 +// assertTrue(createdFile.length()>0); +// this.file = createdFile; +// } +// +// @Test +// void readFileTest(){ +// long data = op.readData(file); // fileCreationTest에서 생성한 파일 사용 +// assertTrue(data>0); +// } +} diff --git "a/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/DisplayNameTest.java" "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/DisplayNameTest.java" new file mode 100644 index 0000000..9f9d2a8 --- /dev/null +++ "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/DisplayNameTest.java" @@ -0,0 +1,26 @@ +package testdriven.chap05; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName(("@DisplayName 테스트")) +public class DisplayNameTest { + @DisplayName("값이 같은지 비교") + @Test + void assertEqualsMethod(){ + + } + + @DisplayName("익셉션 발생 여부 테스트") // 테스트 메모 + @Test + void assertThrowsTest(){ + + } + + @Disabled // 특정 테스트 실행하고 싶지 않을 때 + @Test + void failMethod(){ + + } +} diff --git "a/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/LifecycleTest.java" "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/LifecycleTest.java" new file mode 100644 index 0000000..78b6c7f --- /dev/null +++ "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/LifecycleTest.java" @@ -0,0 +1,33 @@ +package testdriven.chap05; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class LifecycleTest { + public LifecycleTest(){ + System.out.println("new LifecycleTest"); + } + + // 각각 테스트 하나 진행 시작할 때 호출되는 메소드 + @BeforeEach + void setUp(){ + System.out.println("setUp"); + } + + @Test + void a(){ + System.out.println("A"); + } + + @Test + void b(){ + System.out.println("B"); + } + + // 각각 테스트 하나 진행 끝날 때 호출되는 메소드 + @AfterEach + void tearDown(){ + System.out.println("tearDown"); + } +} diff --git "a/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/TestMethod.java" "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/TestMethod.java" new file mode 100644 index 0000000..a96705b --- /dev/null +++ "b/\354\236\245\354\261\204\354\235\200/test-driven/src/test/java/testdriven/chap05/TestMethod.java" @@ -0,0 +1,53 @@ +package testdriven.chap05; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.*; + +public class TestMethod { + @Test + void assertEqualsMethod(){ + LocalDate localDate = LocalDate.now(); + LocalDate localDate1 = LocalDate.now(); + assertEquals(localDate, localDate1); + } + + @Test + void assertEqualsMethod2(){ + assertEquals(3, 5/2); // 에러 발생시, 검증 코드 실패로 + assertEquals(4, 2*2); // 이 코드 실행 되지 않음 + } + +// @Test +// void failMethod(){ +// try{ +// AuthService authService = new AuthService(); +// authService.authenticate(null, null); // null 이 발생했는데, 익셉션이 발생하지 않으면 테스트 실패 (null == null) +// fail(); +// }catch (IllegalArgumentException e){ +// +// } +// } +// +// @Test +// void assertThrowsMethod(){ +// IllegalArgumentException e = assertThrows(IllegalStateException.class, +// ()->{ +// AuthService authService = new AuthService(); +// authService.authenticate(null, null); +// }); +// +// assertTrue(e.getMessage().contains("id")); +// } + + @Test + void assertAllMethod(){ + assertAll( + () -> assertEquals(3, 5/2), + () -> assertEquals(4, 2*2), + () -> assertEquals(6, 11/2) + ); + } +}