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?

seamaster
  • 381
  • 1
  • 4
  • 12
  • 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 – seamaster 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. – seamaster 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

5 Answers5

2

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
  • 29,388
  • 11
  • 94
  • 103
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
  • 21,988
  • 13
  • 81
  • 109
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
  • 35,919
  • 9
  • 100
  • 137
0

In my case I just removed the Eclipse's JUnit5 library from classpath.

dimplex
  • 494
  • 5
  • 9
0

If you get this error in eclipse the problem is that eclipse already has a hamcrest-core library in its plugins folder which has priority over your maven hamcrest library.

What you can do is overwrite the eclipse one (careful to have the same version with the one you have in your maven repositories folder). I am using hamcrest-core 1.3.

I'll show the steps for MacOS but should be similar on Windows you just need to find the eclipse plugins folder and maven repositories folder on your own machine.

  1. Close Eclipse.
  2. Go to Eclipse plugins folder (on MacOS: ~/.p2/pool/plugins)
  3. Find your hamcrest-core library (for me: org.hamcrest.core_1.3.0.v20180420-1519.jar)
  4. Make a backup so that you can revert it - rename the file with a different extension (ex: org.hamcrest.core_1.3.0.v20180420-1519.bck)
  5. Copy from the maven repositories (on MacOS: ~/.m2/repository) the hamcrest-core library into the plugins folder with the same name (on MacOS: cp ~/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar org.hamcrest.core_1.3.0.v20180420-1519.jar)
  6. Open Eclipse and run the test, the test should now run correctly.