2016-05-05 72 views
1

我收到了这样的消息。当JUnit测试失败时显示用户友好的消息

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

测试编译的依赖是

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' 

,这是我的测试代码。

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)); 
    } 
} 

我想要这样的消息。

预期:java.lang.Integer中 的一个实例,但:< 1L>是一个java.lang.Long中

+0

如何使用'assertTrue(“messageIfFail”,condition)'? – ha9u63ar

+1

or'org.junit.Assert.assertThat(String,Object,Matcher <?super Object>)'?第一个参数(字符串)是将被包含在消息中的一个原因。话虽如此,你的测试失败了一个'NoSuchMethodError',而不是一个断言错误,让我觉得你的类路径有问题。 – Taylor

回答

2

java.lang.NoSuchMethodError是不相关的特异性的JUnit。相反,它表示编译器类路径上的类的版本与运行时类路径上的类的版本不同。

冲突的原因是JUnit自带的org.hamcrest.Matcher类正在使用,而不是您的代码中导入的类。在您的导入中使用mockito-core而不是mockito-all来排除匹配器。