Maven 3 and JUnit 4 compilation problem: package org.junit does not exist

前端 未结 16 1469
时光取名叫无心
时光取名叫无心 2020-12-13 12:06

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

相关标签:
16条回答
  • 2020-12-13 12:30

    Me too had the same problem as shown below.

    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'
        }
    }
    
    0 讨论(0)
  • 2020-12-13 12:30

    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

    0 讨论(0)
  • 2020-12-13 12:31

    My case was a simple oversight.

    I put the JUnit dependency declaration inside <dependencies> under the <dependencyManagement/> node instead of <project/> in the POM file. Correct way is:

    <project>
    <!-- Other elements -->
        <dependencies>
        <!-- Other dependencies-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
            </dependency>
        </dependencies>
    <project>
    
    0 讨论(0)
  • 2020-12-13 12:32

    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>
    
    0 讨论(0)
  • 2020-12-13 12:34

    Add this dependency to your pom.xml file:

    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>
    
    0 讨论(0)
  • 2020-12-13 12:36

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

    0 讨论(0)
提交回复
热议问题