사용자 도구

사이트 도구


java:junit:5

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:junit:5 [2019/10/24 17:51]
kwon37xi
java:junit:5 [2022/08/31 10:25] (현재)
kwon37xi
줄 1: 줄 1:
 ====== JUnit 5 ====== ====== JUnit 5 ======
 +  * https://mvnrepository.com/artifact/org.junit
 +  * [[java:assertj|AssertJ]]
 +  * [[java:mockito|Mockito]]
 +  * [[java:junit:junit_pioneer|JUnit Pioneer]]
 +  * [[https://mkyong.com/junit5/junit-5-tutorials/|JUnit 5 Tutorials - Mkyong.com]]
 +
 +===== API 개념 =====
 +  * **테스트 코드 작성을 위한 API**와, 그렇게 작성한 **테스트를 수행하는 Engine**을 분리하였다.
 +
 +
 +==== JUnit 4로 작성한 코드 ====
 +  * JUnit 4 API로 작성한 코드도 JUnit 5에서 실행가능하다.
 +  * 이 경우 [[https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine|junit-vintage-engine]] 으로 해당 코드를 수행한다.
 +
 +==== JUnit 5로 작성한 코드 ====
 +  * JUnit 5는 [[https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api|junit-jupiter-api]]로 테스트 코드를 작성하고
 +  * [[https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine|junit-jupiter-engine]]으로 테스트 코드를 수행한다.
 +
 +
 +===== Gradle =====
 +  * [[https://www.baeldung.com/junit-5-gradle|Using JUnit 5 with Gradle | Baeldung]]
 +  * [[:gradle|Gradle]] **4.6** 버전 이상 사용.
 +  * [[:gradle|Gradle]]과 함께 사용시 <code groovy>
 +test {
 +    useJUnitPlatform()
 +}
 +
 +// spring-boot-starter-test 로 의존성 지정시
 +testImplementation('org.springframework.boot:spring-boot-starter-test') {
 +    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' // junit4 지원 제외
 +}
 +</code>
 +
 +===== 주유 Annotations =====
 +  * ''@DisplayName("설명")'' : 테스트 설명을 일반 텍스트로 지정 가능. 가독성이 높아짐.
 +  * ''@Disabled("설명")'' : 테스트 안하게 함. 설명도 지정.
 +  * ''@Disabled*("설명")'' : ''@Disabled'' 로 시작하는 여러 애노테이션들. 해당 조건에 따라 테스트 안하게 함. 설명도 지정.
 +  * ''@BeforeAll'' : 테스트 클래스 초기화시 한 번 실행
 +  * ''@BeforeEach'' : 모든 테스트 메소드 실행 직전 실행
 +  * ''@Test'' : 테스트
 +  * ''@AfterEach'' : 모든 테스트 메소드 실행 직후 실행
 +  * ''@AfterAll'' : 테스트 클래스가 종료될 때 실행
 +  * ''@Nested'' : 중첩 클래스에 지정. 중첩 테스트가 만들기.
 +  * ''@Tag'' : 클래스나 메서드에 붙인다. 테스트 실행시 특정 태그가 붙은 것만 실행/제외 할 수 있다.
 +
 +===== Assumptions =====
 +  * ''assumTrue'' : boolean 인자가 true 일 때만 테스트 실행
 +  * ''assumFalse'' : boolean 인자가 false 일 때만 테스트 실행
 +  * ''assumeThat(boolean/BooleanSupplier, Executable)'' : boolean 이 true이면 executable lambda 실행
 +
 +===== IntelliJ IDEA JUnit 5 migration =====
 +  * **Refactor -> Migrate Packages and Classes -> JUnit (4.x -> 5.0)** 을 실행하여 리팩토링을 바로 실행할 수 있다.
 +  * 그 이후 ''**@RunWith**(MockitoJUnitRunner.class)'' -> ''**@ExtendWith**(MockitoExtension.class)'' 같은 종류를 찾아서 직접 변경해준다.
 +    * **Structural Replace**를 사용하면 쉽게 된다.
 +    * 변경된 파일들을 모두 선택하고 **Organize Imports(Ctrl+Alt+o)**를 해주는게 좋다.
 +  * 기존 [[java:mockito|Mockito]] 테스트를 전환 할 경우 ''@MockitoSettings(strictness = Strictness.LENIENT)'' 설정이 필요한 경우가 많이 발생했음. 단, 되도록 이 설정을 없애도록 테스트를 수정할것.
 +  * ''Test(expected = SomeException.class)'' 을 JUnit 5의 예외 테스트로 변경한다.
 +    * **Structural Search**를 사용한다.
 +    * Live Templates 에서 다음과 같이 만든다.
 +      * Applicable : ''Java(모두)''
 +      * Abbreviation : ''assertex''
 +      * Description : ''JUnit 5 assertThrows''
 +      * Reformat according to style : check
 +      * Use static import if possible : check
 +      * Shorten FQ names : check
 +      * Edit Variables -> ''EXCEPTION''
 +        * Expression : ''clipboard()''
 +<code>
 +org.junit.jupiter.api.Assertions.assertThrows($EXCEPTION$.class, () -> {
 +$SELECTION$
 +});
 +</code>
 +    * 테스트 대상 예외 클래스의 이름을 복사(''Ctrl+C'')한 뒤에 예외 발생 코드를 선택하고 ''Ctrl+Alt+J''로 단축키 실행.
 +  * ''junit-vintage-engine''와 ''junit:4.x'' 의존성을 완전히 삭제하고서(exclude 처리가 필요할 수 있음), 그상황에서 컴파일 오류 등을 잡아낸다.
 +  * 특히, ''Assert'' 관련해서 junit 4 를 계속보고 있는게 발견되었다. [[https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/Assertions.html|jupiter Assertions]]나 [[java:junit:hamcrest|hamcrest]]와 [[java:assertj|AssertJ]] 등으로 전환한다.
 +  * 테스트 클래스 중에 ''public class''에서 ''public''을 제거하고 package private 로 전환한다. 이 과정에서 공통 테스트 라이브러리는 ''public'' 등으로 유지해야하므로 주의한다.
 +    * package private 으로 전환하면 불필요하게 테스트 클래스가 자동완성으로 나오는 일 등이 줄어든다.
 +==== Inspection 으로 하나씩 변경하기 ====
 +
 +  * [[https://blog.jetbrains.com/idea/2020/08/migrating-from-junit-4-to-junit-5/|Migrating from JUnit 4 to JUnit 5 – IntelliJ IDEA Blog | JetBrains]]
 +  * [[https://www.youtube.com/watch?v=F8UTTTDtbH0|IntelliJ IDEA. Migrating from JUnit 4 to JUnit 5 - YouTube]]
 +  * [[https://youtrack.jetbrains.com/issue/IDEA-291948|Missing "JUnit 4 tests can be JUnit 5" inspection and migration action. : IDEA-291948]]
 +  * [[:intellij_idea|IntelliJ IDEA]] inspection 으로 자동 마이그레이션
 +  * **Settings -> Editor -> Inspections** : **JVM languages -> JUnit 4 test can be JUnit 5** 항목이 켜져 있어야 한다.
 +  * JUnit 4 테스트 클래스에서 ''Alt+Enter'' 로 Inspection 수행해서 **Migrate to JUnit 5** 실행
 +  * ''Alt+Enter''로 inspection 이 잘 안된다면 **Code -> Analyze Code -> Run Inspection by name** 에서 "JUnit 4 tests can be JUnit 5" 를 선택해서 강제 Inspection 을 할 수 있다.
 +
 +===== 참조 =====
   * [[https://dzone.com/articles/lifecycle-of-junit-5s-extension-model|Lifecycle of JUnit 5's Extension Model]]   * [[https://dzone.com/articles/lifecycle-of-junit-5s-extension-model|Lifecycle of JUnit 5's Extension Model]]
   * [[https://dzone.com/articles/embrace-junit5|Embrace JUnit 5]]   * [[https://dzone.com/articles/embrace-junit5|Embrace JUnit 5]]
줄 23: 줄 111:
   * [[https://www.mkyong.com/junit5/junit-5-conditional-test-examples/|JUnit 5 Conditional Test Examples – Mkyong.com]]   * [[https://www.mkyong.com/junit5/junit-5-conditional-test-examples/|JUnit 5 Conditional Test Examples – Mkyong.com]]
   * [[https://www.mkyong.com/junit5/junit-5-tagging-and-filtering-tag-examples/|JUnit 5 Tagging and Filtering, @Tag examples – Mkyong.com]]   * [[https://www.mkyong.com/junit5/junit-5-tagging-and-filtering-tag-examples/|JUnit 5 Tagging and Filtering, @Tag examples – Mkyong.com]]
 +  * [[https://github.com/cheese10yun/blog-sample/blob/master/kotlin-junit5/README.md|blog-sample/README.md at master · cheese10yun/blog-sample]]
 +  * [[https://cheese10yun.github.io/junit5-in-spring/|Junit5 with Spring boot - Yun Blog | 기술 블로그]]
 +  * [[https://www.mkyong.com/junit5/junit-5-timeouts-examples/|JUnit 5 Timeouts Examples – Mkyong.com]]
 +  * [[https://www.mkyong.com/junit5/junit-5-display-names/|JUnit 5 Display Names – Mkyong.com]]
 +  * [[https://www.mkyong.com/junit5/junit-5-assertj-examples/|JUnit 5 + AssertJ examples – Mkyong.com]]
 +  * [[https://johngrib.github.io/wiki/junit5-nested/|JUnit5로 계층 구조의 테스트 코드 작성하기 - 기계인간 John Grib]]
 +  * [[https://mkyong.com/junit5/junit-5-tutorials/|JUnit 5 Tutorials - Mkyong.com]]
 +  * [[https://www.javaworld.com/article/3537563/junit-5-tutorial-part-1-unit-testing-with-junit-5-mockito-and-hamcrest.html|JUnit 5 tutorial, part 1: Unit testing with JUnit 5, Mockito, and Hamcrest | JavaWorld]]
 +  * [[https://www.javaworld.com/article/3543268/junit-5-tutorial-part-2-unit-testing-spring-mvc-with-junit-5.html|JUnit 5 tutorial, part 2: Unit testing Spring MVC with JUnit 5 | JavaWorld]]
 +  * [[https://blogs.oracle.com/javamagazine/junit-5-6-makes-testing-easy-with-new-features|JUnit 5.6 Makes Testing Easy with New Features]]
 +  * [[https://www.baeldung.com/java-testing-system-out-println?utm_medium=feed|Unit Testing of System.out.println() with JUnit | Baeldung]]
 +  * [[https://gmlwjd9405.github.io/2019/11/26/junit5-guide-basic.html|[JUnit] JUnit5 사용법 - 기본 - Heee's Development Blog]]
 +  * [[https://gmlwjd9405.github.io/2019/11/27/junit5-guide-parameterized-test.html|[JUnit] JUnit5 사용법 - Parameterized Tests - Heee's Development Blog]]
 +  * [[https://blog.codecentric.de/en/2018/09/structured-junit-5-testing/|Structured JUnit 5 testing - codecentric AG Blog]]
 +  * [[https://www.javacodegeeks.com/2020/08/extending-junit-5.html|Extending JUnit 5 | Java Code Geeks - 2020]]
 +  * [[https://www.javacodegeeks.com/2020/12/parameterized-tests-in-junit-5.html|Parameterized Tests in JUnit 5 | Java Code Geeks - 2020]]
 +  * [[https://github.com/stefanbirkner/system-lambda|stefanbirkner/system-lambda: System Lambda is a collection of functions for testing code that uses java.lang.System]]
 +  * [[https://www.lambdatest.com/blog/junit5-extensions/|A Comprehensive Guide On JUnit 5 Extensions]]
 +
java/junit/5.1571907070.txt.gz · 마지막으로 수정됨: 2019/10/24 17:51 저자 kwon37xi