Eclipse / Maven : JUnit 테스트를 실행할 때 컴파일되지 않음
Maven과 Eclipse (m2eclipse 플러그인)를 사용하여 프로젝트를 진행하고 있습니다. JUnit 테스트에 문제가 있습니다.
때로는 Eclipse 내에서 실행할 때 컴파일되지 않지만 대신 이전 클래스 파일이 사용됩니다. 클래스 파일을 삭제하면 ClassNotFoundExceptions
Eclipse에 들어갑니다. 그런 다음 mvn test-compile
또는 다른 목표 를 사용하여 수동으로 다시 컴파일해야합니다 .
테스트의 클래스 파일이 때때로에 투입되는 것을 또한 알게 classes
대신 하위 디렉토리 test-classes
.
나는 정말로 무엇이 잘못되었는지 알아낼 수 없다.
JUnit Java 파일은 내부 src/main/java
에 있으며 올바르게 이름이 지정되었습니다 ( *Test.java
).
Maven을 통해 항상 컴파일하고 실행해야합니까? 파일을 실행하려고 할 때 Eclipse가 파일을 컴파일하지 않는 이유는 무엇입니까? (흥미롭게 도 가끔 그렇습니다. 때로는 모든 것이 완벽하게 작동합니다.)
STS Eclipse (Spring development variant), m2e 및 JUnit에서 동일한 문제가 발생했습니다. 해결책은 출력 폴더를 다음 src/test/java
과 target/test-classes
같이 설정하는 것이 었 습니다 .
src/test/java
패키지 탐색기에서 폴더를 마우스 오른쪽 버튼으로 클릭하십시오.- 빌드 경로 선택 -> 출력 폴더 구성
- 을 입력
target/test-classes
하고 확인을 클릭합니다.
이제 테스트 클래스의 변경 사항이 올바르게 컴파일되고 Eclipse에서 JUnit 테스트를 실행할 수 있습니다.
문제는 Eclipse가 단위 테스트를 기본 출력 폴더로 컴파일하는 target/classes
반면 JUnit 플러그인은 test-classes
.
이 질문에 대한 몇 가지 중복이 있습니다.
- Eclipse 내에서 JUnit 단위 테스트를 실행할 때 ClassNotFoundException (Maven 사용)
- Eclipse가 내 새 junit 테스트를 보지 못함
- 최신 파일을 사용하지 않는 junit
아래 답변 외에도
- src / test / java 폴더를 마우스 오른쪽 버튼으로 클릭하십시오.
- 빌드 경로 선택-> 출력 폴더 구성
- 대상 / 테스트 클래스를 입력하고 확인을 클릭합니다.
프로젝트를 마우스 오른쪽 버튼으로 클릭하고 Properties-> Builder로 이동하여 빌더가 올바르게 설정되었는지 확인해야합니다 . 빌더가 누락 된 경우이를 설치해야합니다. 제 경우에는 maven 프로젝트에 AspectJ 종속성이 있었고 Maven Eclipse 플러그인을 사용하여 Eclipse 프로젝트를 빌드 할 때 기본적으로 AspectJ 빌더를 찾고있었습니다. AspectJ 개발 도구를 설치했고 문제가 해결되었습니다.
도움이 되었기를 바랍니다!
그리고 또 다른 요점 : JUnit 테스트 클래스는 src / main / java가 아닌 src / test / java에 있어야합니다. 그렇지 않으면 Maven에서 테스트 클래스로 올바르게 감지되지 않고 패키지 된 jar에 포함되고 테스트 jar에는 포함되지 않습니다. .
직면 한 문제에 대한 가장 가능성있는 설명은 출력 폴더 src/test/java
가 올바르게 구성되지 않았기 때문입니다.
이 구성을 수동으로 수정 하는 대신 m2eclipse가이를 수정 하도록 할 수 있습니다. 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 Maven> 프로젝트 업데이트를 선택하면 됩니다.
나는 같은 문제에 직면했다. 출력 폴더 및 Maven> Update Project 구성에 대한 위의 제안을 시도했지만 둘 다 작동하지 않았습니다. 마지막으로 내 testOutputDirectory를 "빌드 / 클래스"로 변경했으며 이제 단위 테스트가 선택되고 실행됩니다.
마침내 문제의 원인을 찾았습니다. 내 pom에서 우리는 또한 아래와 같이 maven 컴파일러 플러그인을 구성했습니다.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<outputDirectory>build/classes</outputDirectory>
</configuration>
</plugin>
outputDirectory 구성이 필요하지 않으며 위 문제의 원인이었습니다. 이 태그를 제거한 후 junits는 build> testclasses 폴더로 컴파일되고 maven 빌드 중에도 실행됩니다. Yippee :)
pom.xml에서 구성 할 수있는 "testSourceDirectory"경로를 확인하십시오. 그런 다음 이클립스 빌드 경로에 폴더 ( "testSourceDirectory"경로에 구성됨)를 추가합니다.
아래 pom.xml에서 샘플 "testSourceDirectory"를 찾으십시오.
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
프로젝트 아이콘에 느낌표가 있는지 확인하십시오! 제 경우에는 프로젝트 아이콘의 느낌표 와 같은 느낌표가 있다는 것을 무시했습니다.
"마커"퍼스펙티브를 연 다음 팁에 따라 문제를 해결하십시오. "마커"관점이 보여주는 것
junit 테스트 클래스는 "Markers"관점에서 경고되는 읽을 수없는 jar를 참조하지 않기 때문에 "mvn clean test"를 호출 한 후에 성공적으로 실행할 수 있습니다. 따라서 무시하기 쉽습니다 ..
For someone working on java-scala mix project, this is something to note. Even after doing the configuration in the manner shown below,
<build>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
<plugins>
...
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<testSourceDir>${basedir}/src/test/scala</testSourceDir>
<testOutputDir>${basedir}/target/test-classes</testOutputDir>
</configuration>
</plugin>
</plugins>
</build>
and doing a Maven > Update Project, eclipse honours the output directory of the src/test/java folder in the project, however, not for the src/test/scala folder. (You can figure this out by right-clicking on the specific source folder and choosing Build Path > Configure Output Folder... which should display the location as specified in the pom for the former case, however, not for the later case.
This is already a known bug for using scala with m2e as mentioned here: http://scala-ide.org/docs/tutorials/m2eclipse/
Warning
As of March 2013, a bug causes both src/main/scala and src/test/scala to use the default output folder (target/classes). This may be confusing >when building tests, since their class files will not end in target/test-classes, as they would when building on the command line. You can work around this by manually changing the output folder for src/test/scala.
Eclipse is not expecting anything else to be mucking with the class files. It assumes that if you haven't editted the file in eclipse it hasn't changed and doesn't need compiling. I think the issue stems from eclipse and maven sharing an output directory. I've often seen this if my mvn build fails, it will have deleted the class files as part of the clean but not compiled new ones. I think the best solution would be to have seperate build dirs for mvn and eclipse, but I've never look into this.
My problem wasn't the JUnit plugin but rather the configuration in my pom.xml.
After reviewing all the answers to this question, @Gulats's answer implied to me that I should try setting a testOutputDirectory
in my maven-compiler-plugin section, and that did the trick:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testOutputDir>${basedir}/target/test-classes</testOutputDir>
</configuration>
</plugin>
참고URL : https://stackoverflow.com/questions/6087690/eclipse-maven-junit-tests-not-compiled-when-running-them
'Program Tip' 카테고리의 다른 글
Java의 System.exit ()는 try / catch / finally 블록과 어떻게 작동합니까? (0) | 2020.11.08 |
---|---|
git에서 커밋 마스터 포인트를 어떻게 변경할 수 있습니까? (0) | 2020.11.08 |
XML 네임 스페이스는 무엇입니까? (0) | 2020.11.08 |
StackOverflowException을 방지 및 / 또는 처리하려면 어떻게해야합니까? (0) | 2020.11.08 |
브라우저가 gzip 요청을 보낼 수없는 이유는 무엇입니까? (0) | 2020.11.08 |