maven无法从测试用例中找到任何包

时间:2016-08-26 16:30:20

标签: java spring maven unit-testing junit

package my.helloworld;

import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.Test;

public class HomeControllerTest {

}

以上是我的测试代码。即使这个空代码也没有编译。下面是我的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>my.helloworld</groupId>
  <artifactId>spring-helloworld</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring-helloworld</name>
  <url>http://maven.apache.org</url>
  <properties>
    <jdk.version>1.6</jdk.version>
    <spring.version>3.2.13.RELEASE</spring.version>
    <jstl.version>1.2</jstl.version>
  </properties>

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <source>${jdk.version}</source>
      <target>${jdk.version}</target>
    </configuration>
      </plugin>

      <!-- embedded Jetty server, for testing -->
      <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.2.11.v20150529</version>
    <configuration>
      <scanIntervalSeconds>10</scanIntervalSeconds>
      <webApp>
        <contextPath>/spring3</contextPath>
      </webApp>
    </configuration>
      </plugin>

      <!-- configure Eclipse workspace -->
      <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.9</version>
    <configuration>
      <downloadSources>true</downloadSources>
      <downloadJavadocs>true</downloadJavadocs>
      <wtpversion>2.0</wtpversion>
      <wtpContextName>spring3</wtpContextName>
    </configuration>
      </plugin>
    </plugins>
  </build>
</project>

现在,当我运行mvn jetty:runmvn clean installmvn test时,我收到以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:testCompile (default-testCompile) on project spring-helloworld: Compilation failure: Compilation failure:
[ERROR] /home/kay/Projects/Spring/MVC/HelloWorld/spring-helloworld/src/test/java/my/helloworld/HomeControllerTest.java:[3,1] package org.springframework.test.web.servlet.result.MockMvcResultMatchers does not exist
[ERROR] /home/kay/Projects/Spring/MVC/HelloWorld/spring-helloworld/src/test/java/my/helloworld/HomeControllerTest.java:[7,17] package org.junit does not exist
[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

之前,没有找到包org的错误。我在这里缺少什么?

2 个答案:

答案 0 :(得分:0)

您尝试org.springframework.test.web.servlet.result.MockMvcResultMatchers的课程importspring-test模块的一部分。添加它作为测试依赖项,因为否则,它将不在您的类路径上,因此,您的编译器无法找到它。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    <scope>test</scope>
</dependency>

如果你没有导入来自org。*的东西,那么编译器将找不到包org。但是要更准确地说一下,我们需要代码,最好是错误信息。

答案 1 :(得分:0)

您是否尝试删除<scope>test</scope>以确定其是否有效?