【问题标题】:JUnit / Hamcrest - org.hamcrest.CoreMatchers.is() is deprecated. What should I use instead?JUnit / Hamcrest - org.hamcrest.CoreMatchers.is() 已弃用。我应该改用什么?
【发布时间】:2017-09-24 14:51:44
【问题描述】:

不推荐使用org.hamcrest.CoreMatchers.is() 方法。
doc 说使用 - org.hamcrest.CoreMatchers.isA() 代替。

isA() 似乎一起服务于不同的情况。

好的。无论如何,来解决我的问题。早些时候我使用is() 如下

// might be i should not be using it like this, but it works.
assertThat(actualRes, is(true));

现在我不能将它与isA() 一起使用。它抛出编译错误 不适用于参数(布尔值)

我了解isA() 的作用。我想知道的是,鉴于is() 已被弃用,我应该用什么来代替assertThat(actualRes, is(true))

【问题讨论】:

    标签: java junit junit4 hamcrest


    【解决方案1】:

    CoreMatchers.is() 的弃用形式是this one

    是(java.lang.Class类型)

    已弃用。请改用 isA(Class type)。

    因此,对于这个 isA 是正确的替代方案,但您在此断言中使用的 CoreMatchers.is() 的形式:assertThat(actualRes, is(true));this one ...

    是(T值)

    常用is(equalTo(x))的快捷方式。

    ... 已弃用。

    这里有一些代码可以说明问题:

    boolean actualRes = true;
    
    // this passes because the *value of* actualRes is true
    assertThat(actualRes, CoreMatchers.is(true));
    
    // this matcher is deprecated but the assertion still passes because the *type of* actualRes is a Boolean
    assertThat(actualRes, CoreMatchers.is(Boolean.class));
    
    // this passes because the *type of* actualRes is a Boolean
    assertThat(actualRes, CoreMatchers.isA(Boolean.class));
    

    【讨论】:

    • 感谢您的回答。但可悲的是,hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/… 表示它也在贬值。 ?离发疯只有几分钟的路程。任何进一步的建议都可以恢复我。 :-)
    • @samshers 我已经更新了我的答案,简而言之;此方法:is(java.lang.Class<T> type) 已弃用,但您在此断言中使用该方法:assertThat(actualRes, Matchers.is(true));
    • gr8,明白了。发送。
    猜你喜欢
    • 1970-01-01
    • 2019-08-28
    • 2010-09-17
    • 1970-01-01
    • 2014-01-09
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多