gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:00 25 4
gpt4 key购买 nike

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 的错误。我在这里缺少什么?

最佳答案

您尝试导入的类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.*。但为了更准确地说,我们需要代码,最好是错误消息。

关于java - maven 无法从测试用例中找到任何包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39170858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com