Development Tip

Maven 3 및 JUnit 4 컴파일 문제 : org.junit 패키지가 없습니다.

yourdevel 2020. 10. 26. 21:26
반응형

Maven 3 및 JUnit 4 컴파일 문제 : org.junit 패키지가 없습니다.


Maven으로 간단한 Java 프로젝트를 빌드하려고합니다. 내 pom-file에서 JUnit 4.8.2를 유일한 종속성으로 선언합니다. Still Maven은 JUnit 버전 3.8.1 사용을 주장합니다. 어떻게 고치나요?

문제는 컴파일 실패로 나타납니다 : "패키지 org.junit가 존재하지 않습니다". 이것은 내 소스 코드의 import 문 때문입니다. JUnit 4. *의 올바른 패키지 이름은 org.junit. *이고 버전 3. *에서는 junit.framework. *입니다.

http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html 에서 문제의 근원에 대한 문서를 찾았다 고 생각 하지만 거기에있는 조언은 Maven 전문가를위한 것 같습니다. 나는 무엇을 해야할지 이해하지 못했다.


방문자를 돕기위한 완전한 솔루션으로 답변을 얻으려면 :

당신이해야 할 일은 junit 의존성을 pom.xml. 잊지 마세요<scope>test</scope>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>

@Dennis Roberts : 당신은 절대적으로 옳았습니다 : 제 테스트 클래스는 src / main / java에있었습니다. 또한 JUnit 용 POM에있는 "범위"요소의 값은 "테스트"였지만, 그렇게되어 있어야합니다. 문제는 Eclipse에서 테스트 클래스를 만들 때 실수로 인해 src / test / java의 src / main / java에 생성되었습니다. 이것은 "mvn eclipse : eclipse"를 실행 한 후 Eclipse의 프로젝트 탐색기보기에서 더 쉽게 볼 수있게되었습니다. 그러나 귀하의 의견은 제가 처음으로 보게 한 것입니다. 감사.


내 문제는 내 문제 를 해결하는 일반 구조 폴더를 사용하도록 만든이 줄을 제거 pom.xml하는 줄 <sourceDirectory>${basedir}/src</sourceDirectory>내 안에있는 줄이었습니다.


junit의 pom.xml에서 범위 태그 제거가 작동했습니다.


이 종속성을 pom.xml파일에 추가 하십시오.

http://mvnrepository.com/artifact/junit/junit-dep/4.8.2

<!-- https://mvnrepository.com/artifact/junit/junit-dep -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit-dep</artifactId>
    <version>4.8.2</version>
</dependency>

버전을 어떻게 선언 했습니까?

<version>4.8.2</version>

여기에 설명 된이 선언의 의미를 숙지 하십시오 (참고 참조) .

Junit 용 3.8.2와 같은 "일반"버전을 선언 할 때 내부적으로는 "무엇이든 허용하지만 3.8.2 선호"로 표시됩니다. 이는 충돌이 감지되면 Maven이 충돌 알고리즘을 사용하여 최상의 버전을 선택할 수 있음을 의미합니다. [3.8.2]를 지정하면 3.8.2 만 사용되며 다른 것은 사용되지 않습니다.

4.8.2 버전을 강제로 사용하려면

<version>[4.8.2]</version>

프로젝트에 다른 종속성이 없으므로 문제를 일으키는 충돌이 없어야합니다. 저장소에서이 버전을 얻을 수 있다면 첫 번째 선언이 작동합니다. 부모 pom에서 종속성을 상속합니까?


나는 같은 문제가 있었다. 내가 한 것은-pom.xml 파일에서 junit 3.8에 대한 종속성을 삭제하고 junit 4.8에 대한 새 종속성을 추가했습니다. 그런 다음 maven을 청소하고 maven을 설치했습니다. 트릭을했습니다. 확인하기 위해 maven 설치 후 project-> properties-build path-> maven 종속성으로 이동하여 이제 junit 3.8 jar가 사라 졌음을 확인했습니다! 대신 junit 4.8 jar가 나열됩니다. 멋있는!!. 이제 내 테스트는 매력처럼 진행됩니다 ..이게 도움이 되길 ..


올바른 위치에 파일이 있었고 <scope>test</scope>JUnit 종속성 항목에서 제거 하면 문제가 해결되었습니다 (JUnit 4.12를 사용하고 있습니다). test스코프 가 있으면 컴파일 단계에서 종속성이 무시 되었다고 생각합니다 . 이제 전화를 걸어도 모든 것이 작동합니다 mvn test.


제 사건은 단순한 감독이었습니다.

POM 파일 대신 노드 <dependencies>아래에 JUnit 종속성 선언을 넣었습니다 . 올바른 방법은 다음과 같습니다.<dependencyManagement/><project/>

<project>
<!-- Other elements -->
    <dependencies>
    <!-- Other dependencies-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
<project>

종속성을 주입하는 부모 프로젝트의 자식 "테스트 유틸리티"(JUnit에 기능, 규칙 및 어설 션 추가)에서 유사한 문제가 발생했습니다. org.junit.rules 패키지에 따른 클래스는 src / main / java에있었습니다.

그래서 테스트 범위없이 junit에 대한 종속성을 추가했고 문제가 해결되었습니다.

test-util 프로젝트의 pom.xml :

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>

pom.xml of the parent project :

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <scope>test</scope>
</dependency>

I had a similar problem of Eclipse compiling my code just fine but Maven failed when compiling the tests every time despite the fact JUnit was in my list of dependencies and the tests were in /src/test/java/.

In my case, I had the wrong version of JUnit in my list of dependencies. I wrote JUnit4 tests (with annotations) but had JUnit 3.8.x as my dependency. Between version 3.8.x and 4 of JUnit they changed the package name from junit.framework to org.junit which is why Maven still breaks compiling using a JUnit jar.

I'm still not entirely sure why Eclipse successfully compiled. It must have its own copy of JUnit4 somewhere in the classpath. Hope this alternative solution is useful to people. I reached this solution after following Arthur's link above.


I also ran into this issue - I was trying to pull in an object from a source and it was working in the test code but not the src code. To further test, I copied a block of code from the test and dropped it into the src code, then immediately removed the JUnit lines so I just had how the test was pulling in the object. Then suddenly my code wouldn't compile.
The issue was that when I dropped the code in, Eclipse helpfully resolved all the classes so I had JUnit calls coming from my src code, which was not proper. I should have noticed the warnings at the top about unused imports, but I neglected to see them.
Once I removed the unused JUnit imports in my src file, it all worked beautifully.


Me too had the same problem as shown below.

enter image description here

To resolve the issue, below lines are added to dependencies section in the app level build.gradle.

compile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.5'

Gradle build then reported following warning.

Warning:Conflict with dependency 'com.android.support:support-annotations'. 
Resolved versions for app (25.1.0) and test app (23.1.1) differ. 
See http://g.co/androidstudio/app-test-app-conflict for details.

To solve this warning, following section is added to the app level build.gradle.

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:23.1.1'
    }
}

Find the one solution for this error if you have code in src/main/java Utils

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.1</version>
</dependency>

Changing the junit version fixed this for me. Seems like version 3.8.1 didn't work in my case. Issue fixed upon changing it to 4.12


By default , maven looks at these folders for java and test classes respectively - src/main/java and src/test/java

When the src is specified with the test classes under source and the scope for junit dependency in pom.xml is mentioned as test - org.unit will not be found by maven.

참고URL : https://stackoverflow.com/questions/5845990/maven-3-and-junit-4-compilation-problem-package-org-junit-does-not-exist

반응형