6

I'm trying to write some integration tests in my Spring Boot application using REST-Assured and JUnit5 but when I run the following:

@SpringBootTest(classes = ProductsApplication.class)
class ProductsApiTest {

  @Before
  public void setup() {
    RestAssured.baseURI = "http://localhost:8080/test/api/products";
  }

  @Test
  public void test1() {
    ValidatableResponse statusCode = given().when().get().then().statusCode(200);
  }
}

A nasty error comes up:

java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package

Please take a look at my 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        ...
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>   
        <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <scope>test</scope>
        </dependency>
        <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-runner</artifactId>
                    <scope>test</scope>
        </dependency>

            ...
        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
        </dependency>
    </dependencies>

    <build>
        ...
    </build>
</project>

Here are the Order and Export + the Libraries the Eclipse project uses: enter image description here

enter image description here

How do I set up the Eclipse environment to work with REST-Assured and Hamcrest? Why would this exception be thrown?

Stoyan Lupov
  • 341
  • 4
  • 11
  • There are similar questions with accepted answers already: https://stackoverflow.com/a/2877355/906265 https://stackoverflow.com/a/8878106/906265 – Ivar Jan 29 '20 at 09:26
  • is @Before annotation from aspectj or from junit4? – Tymur Kubai aka SirDiR Jan 29 '20 at 09:46
  • @SirDiRakaTymurKubai it is from junit – Stoyan Lupov Jan 29 '20 at 09:47
  • @Aivaras i've seen the article but dont know what to do when i have both Junit (which includes hamcrest) on the class path and spring boot test in maven (which also includes it). Both need to stay but they both include hamcrest. – Stoyan Lupov Jan 29 '20 at 11:00
  • 1
    @StoyanLupov according to your pom.xml you are using junit-jupiter(**junit5**) butt in your code you have @Before(**junit4**) annotation instead of @BeforeEach(**junit5**). I have no idea how you run your tests(what framework used for this junit4 or junit5) and also what are **imports** in your code example – Tymur Kubai aka SirDiR Jan 29 '20 at 12:46
  • @StoyanLupov use `exclusions` in your `pom` next to dependency definition to exclude any transient ones. More in docs https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html – Ivar Jan 29 '20 at 12:50
  • Did you find any solution? It would be really useful to me... Thanks! – Gabriele Morgante Oct 20 '20 at 11:03

3 Answers3

1

I deleted the org.hamcrest.core_1.3.0.v201303031735.jar from my .p2 plugins folder and it worked for me.

  1. I searched for "hamcrest" in "C:\Users\Dell.p2\pool\plugins" folder and found "org.hamcrest.core_1.3.0.v201303031735.jar" there.
  2. I deleted it for this path.
  3. Tried to run the test cases and it got passed with no failure in statusCode() method line.

Please suggest if anyone has a better solution.

Suraj Rao
  • 28,850
  • 10
  • 94
  • 99
0

In my case, the problem was solved by adding an explicit dependency to Hamcrest at the top of the dependency list in my pom.xml:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <scope>test</scope>
</dependency>
Glorfindel
  • 20,880
  • 13
  • 75
  • 99
0

This issue can also occur within Eclipse plugin development if you define your own dependency on a later version of hamcrest.

I worked around this by removing the hamcrest plugin from the platform definition:

  • Select Windows -> Preferences -> Plugin Development -> Running Platform -> Edit -> Content
  • Filter for "hamcrest"
  • Untick org.hamcrest.core
Adam
  • 34,473
  • 9
  • 94
  • 130