Intellij Idea 에서 JUnit 연동하고 사용하기.
intellij 가 버전업이 되면서 기존에 maven 프로제트 생성했을때 main 과 같은 depth 에서 자동으로 생성되던 test 디렉토리가 사라졌다.
이제 단위테스트를 하기 위해서는 수동으로 test 디렉토리를 생성해주어야 한다.
개발환경
JDK 1.8
Spring Framework 4.3.7 RELEASE
MAVEN 3
Windows 10
"C:\Program Files\Java\jdk1.8.0_121\bin\java" -Dmaven.multiModuleProjectDirectory=D:\IdeaProjects\notification "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA 172.3095.12\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA 172.3095.12\plugins\maven\lib\maven3\bin\m2.conf" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 172.3095.12\lib\idea_rt.jar=57837:C:\Program Files\JetBrains\IntelliJ IDEA 172.3095.12\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 172.3095.12\plugins\maven\lib\maven3\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2017.2.4 install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building notification Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ notification ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 11 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ notification ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 12 source files to D:\IdeaProjects\notification\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ notification ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\IdeaProjects\notification\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ notification ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\IdeaProjects\notification\target\test-classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/common/NotificationUtilsTest.java:[4,17] package org.junit does not exist
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/controller/NotificationControllerTest.java:[8,17] package org.junit does not exist
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/common/NotificationUtilsTest.java:[18,6] cannot find symbol
symbol: class Test
location: class kr.co.xxxxxxxx.notification.common.NotificationUtilsTest
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/controller/NotificationControllerTest.java:[25,6] cannot find symbol
symbol: class Test
location: class kr.co.xxxxxxxx.notification.controller.NotificationControllerTest
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.472 s
[INFO] Finished at: 2017-09-05T16:55:06+09:00
[INFO] Final Memory: 20M/197M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project notification: Compilation failure: Compilation failure:
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/common/NotificationUtilsTest.java:[4,17] package org.junit does not exist
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/controller/NotificationControllerTest.java:[8,17] package org.junit does not exist
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/common/NotificationUtilsTest.java:[18,6] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class kr.co.xxxxxxxx.notification.common.NotificationUtilsTest
[ERROR] /D:/IdeaProjects/notification/src/test/java/kr/co/xxxxxxxx/notification/controller/NotificationControllerTest.java:[25,6] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class kr.co.xxxxxxxx.notification.controller.NotificationControllerTest
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Process finished with exit code 1
intellij 는 maven 프로젝트를 생성하면 pom 에 JUnit 자동으로 추가해 주는데 무슨 이유인지는 몰라도 아직까지 version 3.8.1 을 사용한다.
위의 오류는 JUnit 버전을 4.x 로 수정해주면 해결된다.
....
<!-- JUnit. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
....
테스트 클래스에서 테스트하는 방법은 아래와 같다.
....
public class NotificationUtilsTest extends TestCase {
private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
private String TAG = this.getClass().getName();
NotificationUtils notificationUtils = new NotificationUtils();
@Test
public void testGetCertificationNumber() throws Exception {
assertEquals(4, notificationUtils.getCertificationNumber("4").length());
}
public void testSendNotification() throws Exception {
}
}
....
좌측 line number 옆에 테스트를 위한 아이콘이 출력되며 클릭하면 테스트를 수행한다.
또는 Edit Configurations 에서 JUnit 을 추가한 뒤 테스트를 수행하는 방법도 있다.
(환경설정에서 잡아주는 패키지 밑의 모든 테스트 케이스를 실행해준다.)
1. '+' 아이콘을 클릭해서 JUnit4 를 추가한다.
2. Test kind 에 'All in package' 를 선택한다.
3. Package 에 테스트하고자 하는 패키지를 정의한다.
4. 실행하면 위에서 정의한 패키지에 구현한 모든 테스트 클래스를 실행한다.
'Development > IDE' 카테고리의 다른 글
IntelliJ IDEA 에서 SerialVersionUID 자동생성하기. (0) | 2015.05.07 |
---|---|
eclipse 에 maven, spring, android, svn 플러그인 설치하기. (0) | 2013.01.30 |
IntelliJ 커서 설정하기. (0) | 2012.05.30 |
Eclipse Helios 버전에서 Android 개발하면서 자동완성 기능이 너무 느릴때.. (0) | 2010.10.16 |
IntelliJ IDEA 라이브러리 환경 세팅하기. (0) | 2010.03.21 |