我正在关注有关如何在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>
答案 0 :(得分:0)
我想你的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>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
建立一个项目