我试图使用REST Assured v4.3.0
测试控制器类中定义的REST api,但在运行测试时得到了java.lang.AbstractMethodError
。我知道这个错误的发生是因为我调用了一个抽象方法,但我很难解决它
错误似乎是由于SampleControllerTest.java
中的.body(is(equalTo("success")))
而发生的,因为当我删除这一行时,测试成功了。我尝试了一些方法来解决它,但没有取得任何成功:
- 在线搜索建议和示例,但它们要么是旧版本的,要么与
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
Application.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);
}
}
SampleController.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";
}
}
SampleControllerTest.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")));
}
}
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>
<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)
感谢您提前提供的帮助!
由于放心4.3.0将Groovy从2.5.7升级到3.0.2,请参阅master·放心/放心上的放心/changelog.txt,您可以在pom文件中指定Groovy版本。
<properties>
<rest-assured.version>4.3.0</rest-assured.version>
<groovy.version>3.0.2</groovy.version>
</properties>
它看起来像REST Assured 4.3.0中的一个错误,您的代码适用于4.2.0版本。
我没有发现未解决的问题。https://github.com/rest-assured/rest-assured/issues
我用了4.2.0而不是4.3.0。它适用于我的
对于Maven项目,下面的代码可以很好地与Spring Boot项目配合使用:
<properties>
<groovy.version>3.0.7</groovy.version>
<rest-assured.version>4.3.3</rest-assured.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>groovy</artifactId>
<groupId>org.codehaus.groovy</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-xml</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-json</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
```
我也有一个java.lang.AbstractMethodError
,但使用了另一种方法。
放心4.3将其实现升级为使用groovy 3,请参阅放心的发行说明
在我的案例中,依赖解析将groovy降级为使用某个2.x版本。
如果您使用maven:
您可以使用mvn dependency:tree
进行检查。如果存在冲突,请将必要的groovy依赖项作为直接依赖项添加到项目中,版本=>3.0.2。
如果使用渐变:
您可以使用gradle depencencies
进行检查。如果发生冲突,则添加一个解决策略:
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'org.codehaus.groovy') {
details.useVersion "3.0.2"
details.because "needed by rest-assured>=4.3"
}
}
}
也许这能帮助到一些人。
在pom.xml 中使用以下放心依赖项
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
如果使用Rest Assured版本4.3.2
,只需将groovy版本显式地指定为3.0.6
即可修复错误。
<rest-assured.version>4.3.2</rest-assured.version>
<groovy.version>3.0.6</groovy.version>
祝你好运。
原来io.rest-assured/spring-mock-mvc
依赖项与io.rest-assured/rest-assured
依赖项冲突。一旦我从pom.xml
中删除了io.rest-assured/rest-assured
,测试就成功了。
几年前,当我使用RESTAssured版本3.1.1
时,我可以保留这两个依赖项,但可能较新的版本不允许这样做。