Home  >  Q&A  >  body text

<scope>test</scope> causes error when Mavn executes test

When learning maven test, when executing mvn test, org.junit will not be found
It has been introduced in pom.xml

    <dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

The error message is as follows

The file directory is as follows

The following files exist in the hello directory

Where GreeterTest is the test

Executing mvn compile or mvn package will also report an error

When the scope that junit depends on in pom.xml is removed, compilation and testing can be successful.

    <dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

What is the reason for this? Does maven compile *Test files at the same time when executing compile? So why can't mvn test succeed?
Isn’t mvn test automatically executing the *Test file? And scope test determines that junit

will be introduced during testing.
PHPzPHPz2712 days ago936

reply all(1)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-17 10:10:51

    This problem is actually caused by you not being familiar with the Maven file structure. Test classes are generally placed under src/test/java instead of src/main/java. When Maven is compiling, it is placed under src/main/java It does not reference the jar of <scope>test</scope>, but compiling the test under src/test/java will reference the jar of <scope>test</scope>

    reply
    0
  • Cancelreply