How to claim more 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 java.lang.AssertionError and detailMessage when debugging null .

How can I claim more than conditions when using JUnit

+65
java junit
Sep 12 '13 at 13:53 on
source share
6 answers

How did you do that. assertTrue(boolean) also has an overload of assertTrue(String, boolean) , where String is the message in case of failure; you can use this if you want to print that such-and-such is no more than such-and-such.

You can also use matches. 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]))); 

This gives an error, for example:

 java.lang.AssertionError: timestamp Expected: a value greater than <456L> but: <123L> was less than <456L> 
+95
Sep 12 '13 at 13:57 on
source share

When using JUnit statements, I always make the message enjoyable and understandable. This saves a ton of debugging time. Running this method avoids adding the added hamcrest match dependency.

 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); 
+9
Sep 12 '13 at 13:58 on
source share

You must add the Hamcrest library to your build path. It contains the required Matchers.class, which has a lessThan () method.

Dependency as shown below.

 <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> </dependency> 
+5
Sep 11 '16 at 2:05
source share

you can also try under a simple soln:

 previousTokenValues[1] = "1378994409108"; currentTokenValues[1] = "1378994416509"; Long prev = Long.parseLong(previousTokenValues[1]); Long curr = Long.parseLong(currentTokenValues[1]); Assert.assertTrue(prev > curr ); 
+3
Oct 17 '14 at 18:36
source share
 assertTrue("your message", previousTokenValues[1].compareTo(currentTokenValues[1]) > 0) 

this goes for previous> current values

+1
Sep 12 '13 at 14:12
source share

You can write it as

  assertTrue("your fail message ",Long.parseLong(previousTokenValues[1]) > Long.parseLong(currentTokenValues[1])); 
0
Sep 12 '13 at 14:01
source share



All Articles