Java 断言与 JUnit 断言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2966347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
assert vs. JUnit Assertions
提问by Robert
Today I saw a JUnit test case with a java assertion instead of the JUnit assertions—Are there significant advantages or disadvantages to prefer one over the other?
今天我看到了一个 JUnit 测试用例,它带有一个 java 断言而不是 JUnit 断言——选择一个比另一个有明显的优点或缺点吗?
采纳答案by Yishai
In JUnit4 the exception (actually Error) thrown by a JUnit assert is the same as the error thrown by the java assert
keyword (AssertionError), so it is exactly the same as assertTrue
and other than the stack trace you couldn't tell the difference.
在 JUnit4 中,JUnit 断言抛出的异常(实际上是 Error)与 javaassert
关键字(AssertionError)抛出的错误相同,因此它assertTrue
与堆栈跟踪完全相同,除了您无法分辨的区别之外。
That being said, asserts have to run with a special flag in the JVM, causing many tests to appear to pass just because someone forgot to configure the system with that flag when the JUnit tests were run - not good.
话虽如此,断言必须在 JVM 中使用特殊标志运行,导致许多测试似乎通过只是因为有人忘记在运行 JUnit 测试时使用该标志配置系统 - 不好。
In general, because of this, I would argue that using the JUnit assertTrue
is the better practice, because it guarantees the test is run, ensures consistency (you sometimes use assertThat
or other asserts that are not a java keyword) and if the behavior of JUnit asserts should change in the future (such as hooking into some kind of filter or other future JUnit feature) your code will be able to leverage that.
一般来说,因此,我认为使用 JUnitassertTrue
是更好的做法,因为它保证测试运行,确保一致性(您有时使用assertThat
或其他不是 java 关键字的断言)并且如果 JUnit 的行为断言如果将来应该更改(例如挂钩某种过滤器或其他未来的 JUnit 功能),您的代码将能够利用它。
The real purpose of the assert keyword in java is to be able to turn it off without runtime penalty. That doesn't apply to unit tests.
java中assert关键字的真正目的是能够在没有运行时损失的情况下将其关闭。这不适用于单元测试。
回答by Adamski
I prefer JUnit assertions as they offer a richer API than the built-in assert
statement and, more importantly do not need to be explicitly enabled unlike assert
, which requires the -ea
JVM argument.
我更喜欢 JUnit 断言,因为它们提供了比内置assert
语句更丰富的 API ,更重要的是不需要显式启用,不像assert
需要-ea
JVM 参数。
回答by CheesePls
I would say if you are using JUnit you should use the JUnit assertions. assertTrue()
is basically the same as assert
, Otherwise why even use JUnit?
我会说,如果您使用 JUnit,您应该使用 JUnit 断言。assertTrue()
基本一样assert
,不然为什么还要用JUnit呢?
回答by Dan Coates
This may not apply if you exclusively use stuff that's shiny and new, but assert was not introduced into Java until 1.4SE. Therefore, if you must work in an environment with older technology, you may lean towards JUnit for compatibility reasons.
如果您只使用闪亮的新东西,这可能不适用,但断言直到 1.4SE 才被引入 Java。因此,如果您必须在使用较旧技术的环境中工作,出于兼容性原因,您可能会倾向于使用 JUnit。
回答by starmer
When a test fails you get more infomation.
当测试失败时,您会获得更多信息。
assertEquals(1, 2);
results in java.lang.AssertionError: expected:<1> but was:<2>
assertEquals(1, 2);
结果是 java.lang.AssertionError: expected:<1> but was:<2>
vs
对比
assert(1 == 2);
results in java.lang.AssertionError
assert(1 == 2);
结果是 java.lang.AssertionError
you can get even more info if you add the message argument to assertEquals
如果您将 message 参数添加到,您可以获得更多信息 assertEquals
回答by Bruno D. Rodrigues
I'd say use JUnit asserts in test cases, and use java's assert in the code. In other words, real code shall never have JUnit dependencies, as obvious, and if it's a test, it should use the JUnit variations of it, never the assert.
我会说在测试用例中使用 JUnit 断言,并在代码中使用 java 的断言。换句话说,真正的代码永远不会有 JUnit 依赖项,这是显而易见的,如果它是一个测试,它应该使用它的 JUnit 变体,而不是断言。