为什么JUnit不提供assertNotEquals方法

为什么JUnit不提供assertNotEquals方法

本文介绍了为什么JUnit不提供assertNotEquals方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么JUnit 4提供assertEquals(foo,bar)但不提供assertNotEqual(foo,bar)方法吗?

Does anybody know why JUnit 4 provides assertEquals(foo,bar) but not assertNotEqual(foo,bar) methods?

它提供了assertNotSame(对应于assertSame)和assertFalse(对应于assertTrue),因此它们不费力地包含assertNotEqual似乎很奇怪.

It provides assertNotSame (corresponding to assertSame) and assertFalse (corresponding to assertTrue), so it seems strange that they didn't bother including assertNotEqual.

顺便说一句,我知道JUnit插件提供了我正在寻找的方法.我只是出于好奇而问.

By the way, I know that JUnit-addons provides the methods I'm looking for. I'm just asking out of curiosity.

推荐答案

我建议您使用较新的 assertThat() 样式断言,它可以轻松描述各种否定,并自动建立对您期望的结果以及断言失败时得到的结果的描述:

I'd suggest you use the newer assertThat() style asserts, which can easily describe all kinds of negations and automatically build a description of what you expected and what you got if the assertion fails:

assertThat(objectUnderTest, is(not(someOtherObject)));
assertThat(objectUnderTest, not(someOtherObject));
assertThat(objectUnderTest, not(equalTo(someOtherObject)));

这三个选项都是等效的,请选择最容易阅读的一个.

All three options are equivalent, choose the one you find most readable.

要使用方法的简单名称(并允许这种时态语法起作用),您需要进行以下导入:

To use the simple names of the methods (and allow this tense syntax to work), you need these imports:

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

这篇关于为什么JUnit不提供assertNotEquals方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:44