gpt4 book ai didi

java - 在 Java Spring 应用程序中找不到包

转载 作者:行者123 更新时间:2023-12-02 10:06:50 27 4
gpt4 key购买 nike

我正在学习有关如何在 Spring 中运行集成测试的教程,但我已经遇到了一些困难。我所有的导入都说该包不存在。我尝试从 Gradle 切换到 Maven,但仍然不起作用。

这是我尝试运行应用程序时终端的样子:

    src/main/java/cars/CarTest.java:6: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
^
src/main/java/cars/CarTest.java:7: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
^
src/main/java/cars/CarTest.java:8: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
^
src/main/java/cars/CarTest.java:9: error: package org.springframework.beans.factory.annotation does not exist
import org.springframework.beans.factory.annotation.Autowired;
^
src/main/java/cars/CarTest.java:10: error: package org.springframework.beans.factory.annotation does not exist
import org.springframework.beans.factory.annotation.Value;
^
src/main/java/cars/CarTest.java:11: error: package org.springframework.test.context does not exist
import org.springframework.test.context.ContextConfiguration;
^
src/main/java/cars/CarTest.java:12: error: package org.springframework.test.context.junit4 does not exist
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
^
src/main/java/cars/CarTest.java:14: error: cannot find symbol
import static org.hamcrest.Matchers.equalTo;
^
symbol: class Matchers
location: package org.hamcrest
src/main/java/cars/CarTest.java:14: error: static import only from classes and interfaces
import static org.hamcrest.Matchers.equalTo;
^
src/main/java/cars/CarTest.java:15: error: cannot find symbol
import static org.hamcrest.Matchers.is;
^
symbol: class Matchers
location: package org.hamcrest
src/main/java/cars/CarTest.java:15: error: static import only from classes and interfaces
import static org.hamcrest.Matchers.is;
^
src/main/java/cars/CarTest.java:18: error: cannot find symbol
@ContextConfiguration(classes = { ApplicationConfig.class })
^
symbol: class ContextConfiguration
src/main/java/cars/CarTest.java:19: error: cannot find symbol
@WebAppConfiguration
^
symbol: class WebAppConfiguration
src/main/java/cars/CarTest.java:23: error: cannot find symbol
private WebApplicationContext wac;
^
symbol: class WebApplicationContext
location: class CarTest
src/main/java/cars/CarTest.java:25: error: cannot find symbol
private MockMvc mockMvc;
^
symbol: class MockMvc
location: class CarTest
src/main/java/cars/CarTest.java:17: error: cannot find symbol
@RunWith(SpringJUnit4ClassRunner.class)
^
symbol: class SpringJUnit4ClassRunner
src/main/java/cars/CarTest.java:18: error: cannot find symbol
@ContextConfiguration(classes = { ApplicationConfig.class })
^
symbol: class ApplicationConfig
src/main/java/cars/CarTest.java:22: error: cannot find symbol
@Autowired
^
symbol: class Autowired
location: class CarTest
src/main/java/cars/CarTest.java:26: error: cannot find symbol
@Before
^
symbol: class Before
location: class CarTest
src/main/java/cars/CarTest.java:28: error: cannot find symbol
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
^
symbol: variable MockMvcBuilders
location: class CarTest
src/main/java/cars/CarTest.java:33: error: cannot find symbol
ServletContext servletContext = wac.getServletContext();
^
symbol: class ServletContext
location: class CarTest
src/main/java/cars/CarTest.java:36: error: cannot find symbol
Assert.assertTrue(servletContext instanceof MockServletContext);
^
symbol: class MockServletContext
location: class CarTest
src/main/java/cars/CarTest.java:43: error: cannot find symbol
.perform(get("/hello"))
^
symbol: method get(String)
location: class CarTest
src/main/java/cars/CarTest.java:44: error: cannot find symbol
.andDo(print()).andExpect(status().isOk())
^
symbol: method print()
location: class CarTest
src/main/java/cars/CarTest.java:44: error: cannot find symbol
.andDo(print()).andExpect(status().isOk())
^
symbol: method status()
location: class CarTest
src/main/java/cars/CarTest.java:45: error: cannot find symbol
.andExpect(content().contentType("application/json;charset=UTF-8"))
^
symbol: method content()
location: class CarTest
src/main/java/cars/CarTest.java:46: error: cannot find symbol
.andExpect(jsonPath("$.manufacturer").value("Nissan"))
^
symbol: method jsonPath(String)
location: class CarTest
src/main/java/cars/CarTest.java:47: error: cannot find symbol
.andExpect(jsonPath("$.name").value("GT"));
^
symbol: method jsonPath(String)
location: class CarTest

正如你所看到的,一切都没有找到,作为一个初学者,这让我发疯。我知道我可能犯了一个小错误,但我真的很感激一些帮助,谢谢。

下面是我的集成测试文件:

    package cars;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class })
@WebAppConfiguration
public class CarTest {

@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}

@Test
public void givenWac_whenServletContext_thenItProvidesGreetController() {
ServletContext servletContext = wac.getServletContext();

Assert.assertNotNull(servletContext);
Assert.assertTrue(servletContext instanceof MockServletContext);
Assert.assertNotNull(wac.getBean("carController"));
}

@Test
public void checkHelloResponse() throws Exception {
this.mockMvc
.perform(get("/hello"))
.andDo(print()).andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$.manufacturer").value("Nissan"))
.andExpect(jsonPath("$.name").value("GT"));
}
}

最后是 pom.xml,文件:

    <?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

最佳答案

我猜你的 pom.xml 都搞砸了。

我将尝试根据您的使用情况添加示例 jar,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>

请更改groupId artifactId根据您的项目包

我还注意到你的CarTest类有包cars所以我猜你的项目包也搞砸了。

在这种情况下,我建议尝试从 here 构建一个项目

关于java - 在 Java Spring 应用程序中找不到包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55284316/

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