【问题标题】:maven is unable to find any package from test casesmaven 无法从测试用例中找到任何包
【发布时间】:2016-08-26 16:30:20
【问题描述】:
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 的错误。我在这里错过了什么?

【问题讨论】:

    标签: java spring maven unit-testing junit


    【解决方案1】:

    您尝试使用import 的类org.springframework.test.web.servlet.result.MockMvcResultMatchersspring-test 模块的一部分。将其添加为测试依赖项,否则,它将不在您的类路径中,因此您的编译器找不到它。

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

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

    【讨论】:

    • 我添加了这个,但没有帮助
    • 如果你得到同样的错误信息,那么你没有正确添加它。抱歉,关于这个没什么好说的了……这个类是从 Spring 3.2 开始的,所以你不太可能有旧版本……
    【解决方案2】:

    您是否尝试过删除 &lt;scope&gt;test&lt;/scope&gt; 以查看它是否有效?

    【讨论】:

      猜你喜欢
      • 2019-10-16
      • 1970-01-01
      • 2018-04-08
      • 2015-05-22
      • 2022-01-06
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2019-03-19
      相关资源
      最近更新 更多