Maven 3 and JUnit 4 compilation problem: package o

2019-01-16 22:30发布

I am trying to build a simple Java project with Maven. In my pom-file I declare JUnit 4.8.2 as the only dependency. Still Maven insists on using JUnit version 3.8.1. How do I fix it?

The problem manifests itself in a compilation failure: "package org.junit does not exist". This is because of the import statement in my source code. The correct package name in JUnit 4.* is org.junit.* while in version 3.* it is junit.framework.*

I think I have found documentation on the root of the problem on http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html but the advice there seems to be meant for Maven experts. I did not understand what to do.

16条回答
Viruses.
2楼-- · 2019-01-16 22:47

removing the scope tag in pom.xml for junit worked..

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-16 22:49

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.

查看更多
别忘想泡老子
4楼-- · 2019-01-16 22:49

Just to have an answer with the complete solution to help the visitors:

All you need to do is add the junit dependency to pom.xml. Don't forget the <scope>test</scope>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>
查看更多
Viruses.
5楼-- · 2019-01-16 22:51

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'
    }
}
查看更多
孤傲高冷的网名
6楼-- · 2019-01-16 22:51

I had the same problem. All i did was - From the pom.xml file i deleted the dependency for junit 3.8 and added a new dependency for junit 4.8. Then i did maven clean and maven install. It did the trick. To verify , after maven install i went project->properties-build path->maven dependencies and saw that now the junit 3.8 jar is gone !, instead junit 4.8 jar is listed. cool!!. Now my test runs like a charm.. Hope this helps somehow..

查看更多
虎瘦雄心在
7楼-- · 2019-01-16 22:54

I had my files at the correct places, and just removing <scope>test</scope> from the JUnit dependency entry solved the problem (I am using JUnit 4.12). I believe that with the test scope the dependency was just being ignored during the compilation phase. Now everything is working even when I call mvn test.

查看更多
登录 后发表回答