[英]How to assert greater than using JUnit Assert?
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.AssertionError
and detailMessage
on debugging is null
.我在调试时得到
java.lang.AssertionError
和detailMessage
为null
。
How can I assert greater than conditions in using JUnit
如何在使用
JUnit
断言大于条件
Just how you've done it.只是你是如何做到的。
assertTrue(boolean)
also has an overload assertTrue(String, boolean)
where the String
is the message in case of failure; assertTrue(boolean)
也有一个重载assertTrue(String, boolean)
,其中String
是失败时的消息; you can use that if you want to print that such-and-such wasn't greater than so-and-so.如果你想打印某某不大于某某,你可以使用它。
You could also add hamcrest-all
as a dependency to use matchers.您还可以添加
hamcrest-all
作为使用匹配器的依赖项。 See https://code.google.com/p/hamcrest/wiki/Tutorial :请参阅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>
When using JUnit asserts, I always make the message nice and clear.使用 JUnit 断言时,我总是使消息清晰明了。 It saves huge amounts of time debugging.
它节省了大量的调试时间。 Doing it this way avoids having to add a added dependency on hamcrest Matchers.
这样做可以避免添加对 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);
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 );
You should add Hamcrest-library to your Build Path.您应该将 Hamcrest-library 添加到您的构建路径中。 It contains the needed Matchers.class which has the lessThan() method.
它包含所需的 Matchers.class,它具有 lessThan() 方法。
Dependency as below.依赖如下。
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
Alternatively if adding extra library such as hamcrest
is not desirable, the logic can be implemented as utility method using junit
dependency 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 + ">");
}
}
assertTrue("your message", previousTokenValues[1].compareTo(currentTokenValues[1]) > 0)
这通过前 > 当前值
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.意思是,条件在消息前面。
你可以这样说
assertTrue("your fail message ",Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.