如何断言比使用JUnit声明更大的断言? [英] How to assert greater than using JUnit Assert?

查看:70
本文介绍了如何断言比使用JUnit声明更大的断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些值来自测试

previousTokenValues[1] = "1378994409108"
currentTokenValues[1] = "1378994416509"

我尝试

    // current timestamp is greater
    assertTrue(Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1]));

我在调试时得到java.lang.AssertionErrordetailMessagenull.

在使用JUnit

推荐答案

只是您的操作方式. assertTrue(boolean)也有一个超载 assertTrue(String, boolean) ,其中String是失败时的消息;如果您要打印某某某物的大小不超过某某某物,则可以使用它.

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; you can use that if you want to print that such-and-such wasn't greater than so-and-so.

您也可以将hamcrest-all添加为依赖项以使用匹配器.请参见 https://code.google.com/p/hamcrest/wiki/Tutorial:

You could also add hamcrest-all as a dependency to use matchers. See 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])));

出现类似以下错误:

java.lang.AssertionError: timestamp
Expected: a value greater than <456L>
     but: <123L> was less than <456L>

这篇关于如何断言比使用JUnit声明更大的断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆