사용자 도구

사이트 도구


java:junit:5

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
마지막 판 양쪽 다음 판
java:junit:5 [2022/08/22 13:57]
kwon37xi [IntelliJ 에서 자동 migration]
java:junit:5 [2022/08/23 13:08]
kwon37xi [IntelliJ IDEA JUnit 5 migration]
줄 49: 줄 49:
   * ''assumeThat(boolean/BooleanSupplier, Executable)'' : boolean 이 true이면 executable lambda 실행   * ''assumeThat(boolean/BooleanSupplier, Executable)'' : boolean 이 true이면 executable lambda 실행
  
-===== IntelliJ 에서 자동 migration ===== +===== IntelliJ IDEA JUnit 5 migration ===== 
-  *   * [[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]]+  * **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://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]]   * [[https://youtrack.jetbrains.com/issue/IDEA-291948|Missing "JUnit 4 tests can be JUnit 5" inspection and migration action. : IDEA-291948]]
줄 57: 줄 85:
   * JUnit 4 테스트 클래스에서 ''Alt+Enter'' 로 Inspection 수행해서 **Migrate to 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 을 할 수 있다.   * ''Alt+Enter''로 inspection 이 잘 안된다면 **Code -> Analyze Code -> Run Inspection by name** 에서 "JUnit 4 tests can be JUnit 5" 를 선택해서 강제 Inspection 을 할 수 있다.
-  * 혹은 **Refactor -> Migrate Packages and Classes -> JUnit (4.x -> 5.0)** 을 실행하여 리팩토링을 바로 실행할 수도 있다. 
  
 ===== 참조 ===== ===== 참조 =====
java/junit/5.txt · 마지막으로 수정됨: 2022/08/31 10:25 저자 kwon37xi