Maven在Eclipse中运行JUnit测试,但编译在命令行中失败

时间:2015-12-16 02:16:27

标签: java eclipse maven junit

我在Eclipse中创建了一个非常简单的项目来运行一个非常简单的JUnit测试。从Eclipse中执行测试时,这些测试工作正常,但是当通过maven命令行运行时,代码无法编译。

我用来运行maven的命令只是'maven test'。以下是maven输出:

MacBook-Pro:leonard-reference-test randy$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building leonard-reference-test 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ leonard-reference-test ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ leonard-reference-test ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/randy/PSH/workspaces/playground/leonard-reference-test/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/randy/PSH/workspaces/playground/leonard-reference-test/src/main/java/info/leonard/reference/test/ReferenceTestClient.java:[3,17] package org.junit does not exist
[ERROR] /Users/randy/PSH/workspaces/playground/leonard-reference-test/src/main/java/info/leonard/reference/test/ReferenceTestClient.java:[8,10] cannot find symbol
  symbol:   class Test
  location: class info.leonard.reference.test.ReferenceTestClient
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.043 s
[INFO] Finished at: 2015-12-15T19:10:49-07:00
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile (default-compile) on project leonard-reference-test: Compilation failure: Compilation failure:
[ERROR] /Users/randy/PSH/workspaces/playground/leonard-reference-test/src/main/java/info/leonard/reference/test/ReferenceTestClient.java:[3,17] package org.junit does not exist
[ERROR] /Users/randy/PSH/workspaces/playground/leonard-reference-test/src/main/java/info/leonard/reference/test/ReferenceTestClient.java:[8,10] cannot find symbol
[ERROR] symbol:   class Test
[ERROR] location: class info.leonard.reference.test.ReferenceTestClient
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
MacBook-Pro:leonard-reference-test randy$

这个的pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>info.leonard.reference</groupId>
    <artifactId>leonard-reference-test</artifactId>
    <version>0.0.1</version>

    <prerequisites>
        <maven>3.0</maven>
    </prerequisites>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
            </plugin>
        </plugins>
    </build>

</project>

项目中唯一的Java文件如下:

package info.leonard.reference.test;

import org.junit.Test;

public class ReferenceTestClient
{
    @Test
    public void testOne()
    {
        System.out.println("simple test");
    }
}

就是这样......项目中没有其他文件。 Maven测试在Eclipse中成功运行,但代码甚至不能在Maven中编译。

对此简单项目的有效pom.xml文件或其他修改表示赞赏。

2 个答案:

答案 0 :(得分:4)

您的文件位于src/main/java/目录中,但JUnit依赖项的范围是test。这意味着maven在编译非测试代码时不会考虑它。请将您的文件放在src/test/java

答案 1 :(得分:1)

测试范围意味着junit仅在src / test / java

中可用

移动

/Users/randy/PSH/workspaces/playground/leonard-reference-test/src/main/java/info/leonard/reference/test/ReferenceTestClient.java:

/Users/randy/PSH/workspaces/playground/leonard-reference-test/src/test/java/info/leonard/reference/test/ReferenceTestClient.java: