Angle Tom
Angle Tom

Reputation: 1130

Display a user-friendly message when JUnit tests fails

I got the message like this.

com.controller.Test1 > test FAILED
java.lang.NoSuchMethodError at Test1.java:18

The dependencies for test compiling is

testCompile "junit:junit:4.11",
        'org.mockito:mockito-all:1.10.19',
        'com.jayway.jsonpath:json-path:2.2.0',
        'org.hamcrest:hamcrest-all:1.3',
        'org.flywaydb.flyway-test-extensions:flyway-dbunit-spring4-test:4.0'

And this is my test code.

package com.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;


@RunWith(MockitoJUnitRunner.class)
public class Test1 {

   @Test
   public void test(){
      assertThat(Long.valueOf(1), instanceOf(Integer.class));
    }
}

I want the message like this.

Expected: an instance of java.lang.Integer but: <1L> is a java.lang.Long

Upvotes: 1

Views: 165

Answers (1)

Hendrikvh
Hendrikvh

Reputation: 545

The java.lang.NoSuchMethodError is not related to JUnit specifically. Instead, it indicates that the version of a class that was on your compiler's classpath is different from the version of the class that is on your runtime classpath.

The reason for the clash is that JUnit comes with its own org.hamcrest.Matcher class that is being used instead of the one imported in your code. Use mockito-core in your imports instead of mockito-all to exclude the matcher.

Upvotes: 2

Related Questions