Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting java.lang.AbstractMethodError when I test using REST Assured

I'm trying to test a REST api defined in my controller class using REST Assured v4.3.0, but I get java.lang.AbstractMethodError when I run the test. I understand this error is occurring because I'm calling an abstract method, but I'm having a hard time resolving it.

It seems that the error is occurring due to .body(is(equalTo("success"))) in SampleControllerTest.java because when I remove this line, the test succeeds. I tried a few things to resolve it, but didn't get any success:

  • Searched online for suggestions and examples, but they are either for older versions or not related to io.rest-assured/spring-mock-mvc
  • Tried different matchers (org.hamcrest.Matchers.* and org.hamcrest.CoreMatchers.*)
  • Tried adding org.hamcrest/hamcrest dependency in the pom file explicitly

Here's my code for your reference:

Code structure:

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>

When I run the test using mvn test, this is the error I get:

[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)

Thanks for any help in advance!

like image 655
pgngp Avatar asked Apr 03 '20 20:04

pgngp


People also ask

What is Java Lang AbstractMethodError?

java.lang.AbstractMethodError. Thrown when an application tries to call an abstract method. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.

Does rest assured only support Java programming language?

REST Assured is a Java library that provides users with Domain Specific Language (DSL) to create powerful tests for REST APIs in an effective manner. One can test an application written in any language like Java, . Net, Python, Ruby, etc.

Is Rest assured a integration test?

We can use REST Assured to test the REST API of any Java project as it is framework-independent. However, REST Assured comes with an excellent Spring integration for testing our @RestController endpoints that we're about to explore with this article.

What is rest assured in spring boot?

REST Assured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests and can be used to validate and verify the response of these requests.

What is rest assured in Java?

Note: Since the amount value is in string data type, we convert to integer and use it for summation. Rest Assured is a group of java libraries which enables us to automate Rest API testing The API request can be customized with a variety of header, query, path param, and any session or cookies to be set.

Is rest assured API automation testing worth learning?

For the testing community, API Automation Testing is still new and niche. The JSON complexities keep API testing unexplored. But that does not make it less important in the testing process. Rest Assured.io framework has made it very simple using core java basics, making it a very desirable thing to learn. What is Rest Assured?

What is a rest assured test?

REST Assured is an open-sourced Java library that facilitates automated testing of API endpoints. For this post I want to walk through the steps you need to get some basic REST Assured tests up and running on your local machine. Before diving into the nuts and bolts of REST Assured, what is API automated testing?

What is rest-assured in Java with Maven?

Rest-Assured is an open-source Java Domain-specific language (DSL) that makes testing REST service simple. It simplifies things by eliminating the need to use boiler-plate code to test and validate complex responses. It also supports XML and JSON Request/Responses. Let's take a quick look at how to get started using Rest-Assured in Java with Maven.


3 Answers

Because of rest-assured 4.3.0 upgrade the Groovy from 2.5.7 to 3.0.2, see rest-assured/changelog.txt at master · rest-assured/rest-assured, you can specified the groovy version in pom file.

  <properties>
    <rest-assured.version>4.3.0</rest-assured.version>
    <groovy.version>3.0.2</groovy.version>
  </properties>
like image 113
cheng1243 Avatar answered Nov 10 '22 20:11

cheng1243


It looks like a bug in REST Assured 4.3.0, your code works with 4.2.0 version.

I didn't find opened issue. https://github.com/rest-assured/rest-assured/issues

like image 21
Yan Avatar answered Nov 10 '22 20:11

Yan


I used 4.2.0 instead of 4.3.0 .. it works for me

like image 37
zxinyan Avatar answered Nov 10 '22 19:11

zxinyan