Java 如何断言大于使用 JUnit 断言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18766235/
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
How to assert greater than using JUnit Assert?
提问by daydreamer
I have these values coming from a test
我有这些值来自测试
previousTokenValues[1] = "1378994409108"
currentTokenValues[1] = "1378994416509"
and I try
我尝试
// current timestamp is greater
assertTrue(Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1]));
I get the java.lang.AssertionErrorand detailMessageon debugging is null.
我得到java.lang.AssertionError和detailMessage调试是null。
How can I assert greater than conditions in using JUnit
我如何断言大于使用条件 JUnit
采纳答案by yshavit
Just how you've done it. assertTrue(boolean)also has an overload assertTrue(String, boolean)where the Stringis the message in case of failure; you can use that if you want to print that such-and-such wasn't greater than so-and-so.
只是你是如何做到的。assertTrue(boolean)也有一个过载assertTrue(String, boolean),它String是失败时的消息;如果你想打印某某不大于某某,你可以使用它。
You could also add hamcrest-allas a dependency to use matchers. See https://code.google.com/p/hamcrest/wiki/Tutorial:
您还可以添加hamcrest-all为依赖项以使用匹配器。请参阅https://code.google.com/p/hamcrest/wiki/Tutorial:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
assertThat("timestamp",
Long.parseLong(previousTokenValues[1]),
greaterThan(Long.parseLong(currentTokenValues[1])));
That gives an error like:
这给出了一个错误,如:
java.lang.AssertionError: timestamp
Expected: a value greater than <456L>
but: <123L> was less than <456L>
回答by Qwerky
When using JUnit asserts, I always make the message nice and clear. It saves huge amounts of time debugging. Doing it this way avoids having to add a added dependency on hamcrest Matchers.
使用 JUnit 断言时,我总是使消息清晰明了。它节省了大量的调试时间。这样做可以避免添加对 hamcrest Matchers 的额外依赖。
previousTokenValues[1] = "1378994409108";
currentTokenValues[1] = "1378994416509";
Long prev = Long.parseLong(previousTokenValues[1]);
Long curr = Long.parseLong(currentTokenValues[1]);
assertTrue("Previous (" + prev + ") should be greater than current (" + curr + ")", prev > curr);
回答by blackbird014
You can put it like this
你可以这样说
assertTrue("your fail message ",Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1]));
回答by Dave Richardson
assertTrue("your message", previousTokenValues[1].compareTo(currentTokenValues[1]) > 0)
this passes for previous > current values
这适用于以前的 > 当前值
回答by user3293666
you can also try below simple soln:
您也可以尝试以下简单的解决方案:
previousTokenValues[1] = "1378994409108";
currentTokenValues[1] = "1378994416509";
Long prev = Long.parseLong(previousTokenValues[1]);
Long curr = Long.parseLong(currentTokenValues[1]);
Assert.assertTrue(prev > curr );
回答by u6097773
You should add Hamcrest-library to your Build Path. It contains the needed Matchers.class which has the lessThan() method.
您应该将 Hamcrest-library 添加到您的构建路径中。它包含所需的 Matchers.class,它具有 lessThan() 方法。
Dependency as below.
依赖如下。
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
回答by Andrey
Alternatively if adding extra library such as hamcrestis not desirable, the logic can be implemented as utility method using junitdependency only:
或者,如果hamcrest不希望添加额外的库,则可以将逻辑实现为junit仅使用依赖项的实用程序方法:
public static void assertGreaterThan(int greater, int lesser) {
assertGreaterThan(greater, lesser, null);
}
public static void assertGreaterThan(int greater, int lesser, String message) {
if (greater <= lesser) {
fail((StringUtils.isNotBlank(message) ? message + " ==> " : "") +
"Expected: a value greater than <" + lesser + ">\n" +
"But <" + greater + "> was " + (greater == lesser ? "equal to" : "less than") + " <" + lesser + ">");
}
}
回答by Thanh Huy Le
As I recognize, at the moment, in JUnit, the syntax is like this:
据我所知,目前在 JUnit 中,语法是这样的:
AssertTrue(Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1]), "your fail message ");
Means that, the condition is in front of the message.
意思是,条件在消息前面。

