daydreamer daydreamer - 1 year ago 297
Java Question

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
.

How can I assert greater than conditions in using
JUnit

Answer Source

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.

You could also use matchers. See https://code.google.com/p/hamcrest/wiki/Tutorial:

import static org.hamcrest.Matchers.*;

assertThat("timestamp",
           Long.parseLong(previousTokenValues[1]),
           greaterThan(Long.parseLong(currentTokenValues[1])));

That gives an error like:

java.lang.AssertionError: one
Expected: a value greater than <456L>
     got: <123L>