Getting java.lang.AbstractMethodError when I test using REST Assured
2022-09-03 03:51:35
我正在尝试使用 测试在我的控制器类中定义的 REST api,但是当我运行测试时,我得到了。我知道发生此错误是因为我调用了一个抽象方法,但我很难解决它。REST Assured v4.3.0
java.lang.AbstractMethodError
似乎由于 in 而发生错误是因为当我删除此行时,测试成功。我尝试了一些事情来解决它,但没有获得任何成功:.body(is(equalTo("success")))
SampleControllerTest.java
- 在线搜索建议和示例,但它们要么是旧版本,要么与
io.rest-assured/spring-mock-mvc
- 已尝试不同的匹配器( 和
org.hamcrest.Matchers.*
org.hamcrest.CoreMatchers.*
) - 已尝试在 pom 文件中显式添加依赖项
org.hamcrest/hamcrest
这是我的代码供您参考:
代码结构:
test
|- src/
| |- main/
| | |- java/
| | | |- org/
| | | | |- example/
| | | | | |- Application.java
| | | | | |- SampleController.java
| |- test/
| | |- java/
| | | |- org/
| | | | |- example/
| | | | | |- SampleControllerTest.java
|- target/
|- pom.xml
应用.java
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
样品控制器.java
package org.example;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@RequestMapping(value = "/sample")
@ResponseStatus(value = HttpStatus.OK)
public String getSample() {
return "success";
}
}
样品控制器测试.java
package org.example;
import org.junit.Test;
import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class SampleControllerTest {
@Test
public void testGetSample() {
given()
.standaloneSetup(new SampleController())
.when()
.get("/sample")
.then()
.assertThat(status().isOk())
.body(is(equalTo("success")));
}
}
啪.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>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>8</java.version>
<start-class>org.example.Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/spring-mock-mvc -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/xml-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当我使用运行测试时,这是我得到的错误:mvn test
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.325 s <<< FAILURE! - in org.example.SampleControllerTest
[ERROR] testGetSample(org.example.SampleControllerTest) Time elapsed: 1.288 s <<< ERROR!
java.lang.AbstractMethodError: Method io/restassured/internal/ResponseSpecificationImpl.getProperty(Ljava/lang/String;)Ljava/lang/Object; is abstract
at org.example.SampleControllerTest.testGetSample(SampleControllerTest.java:20)
提前感谢您的任何帮助!