154

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

daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • Post the entire code as well with the complete error message. Perhaps you're assertion statement is before the array initialization. – Josh M Sep 12 '13 at 13:58

8 Answers8

197

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 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])));

That gives an error like:

java.lang.AssertionError: timestamp
Expected: a value greater than <456L>
     but: <123L> was less than <456L>
Ben McCann
  • 18,548
  • 25
  • 83
  • 101
yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Thank you for pointing out the Hamcrest solution. I hate it when tests output `expected true got false` when the test is comparing integer values. – John B Sep 12 '13 at 14:54
  • 3
    FYI, here is the link to `OrderingComparison` which contains `greaterThan`: http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/number/OrderingComparison.html – John B Sep 12 '13 at 14:56
  • 7
    `org.hamcrest`, `hamcrest-all` – gavenkoa Mar 19 '14 at 12:53
  • 2
    Note that Hamcrest is included by default when you use JUnit 4.11 so no need to search for the dependency. – Chanoch Apr 05 '14 at 18:48
  • 3
    Be careful with the [order of hamcrest and junit references](http://stackoverflow.com/a/9659820/4794). – Don Kirkby Jun 19 '14 at 05:37
  • @Chanoch I think you need hamcrest-all to get Matchers. JUnit only pulls in hamcrest-core which doesn't have greaterThan. – Anthony Hayward Oct 16 '15 at 11:27
  • @yshavit , I recevie a slightly different error when I run this code. `java.lang.AssertionError: timestamp Expected: a value greater than <456L> got: <123L> but: <123L> was less than <456L>`. Which version of java are you using? – Eric Oct 18 '15 at 20:29
  • @Eric This was a couple years ago, but I probably had it on Java 6. I don't think that's the main factor, though; the version of Hamstring (and possibly JUnit?) is probably more relevant. And I'm not sure which of those I was using. – yshavit Oct 18 '15 at 22:11
  • @AnthonyHayward I think this was in 4.10 though - I tested this out at the time and it worked for me. Doing a quick search now, there are a number of articles from the time talking about the complex dependencies with the junit-dep and hamcrest-library libraries which all seem to have been resolved by 4.11. – Chanoch Nov 03 '15 at 11:52
  • 11
    @Chanoch I have junit 4.12 which transitively depends on hamcrest-core 1.3. There is no greaterThan method on org.hamcrest.CoreMatchers class. If I add hamcrest-all 1.3 as an additional dependency, it provides org.hamcrest.Matchers.greaterThan method. – Anthony Hayward Nov 04 '15 at 17:42
  • I had to use `import static org.hamcrest.number.OrderingComparison.*` so that it works. – tarekahf Jan 16 '23 at 01:11
30

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.

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);
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Qwerky
  • 18,217
  • 6
  • 44
  • 80
17

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 );   
user3293666
  • 333
  • 1
  • 3
  • 12
12

You should add Hamcrest-library to your Build Path. It contains the needed Matchers.class which has the lessThan() method.

Dependency as below.

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-library</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>
Community
  • 1
  • 1
Jiaheng Tao
  • 143
  • 1
  • 8
5

As I recognize, at the moment, in JUnit, the syntax is like this:

AssertTrue(Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1]), "your fail message ");

Means that, the condition is in front of the message.

Thanh Huy Le
  • 89
  • 1
  • 8
3

Alternatively if adding extra library such as hamcrest is not desirable, the logic can be implemented as utility method using junit dependency only:

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 + ">");
    }
}
Andrey
  • 6,526
  • 3
  • 39
  • 58
2
assertTrue("your message", previousTokenValues[1].compareTo(currentTokenValues[1]) > 0)

this passes for previous > current values

Dave Richardson
  • 4,880
  • 7
  • 32
  • 47
0

You can put it like this

  assertTrue("your fail message ",Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1]));
blackbird014
  • 2,069
  • 1
  • 18
  • 23