Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit: less-than assertion?

Is there anything like assertThat(a, lessThan(b)); ? I'm currently using Junit 4.8.1 and I haven't been able to find lessThan. Instead I have to do assertTrue(a < b), but this has a drawback that it doesn't print two numbers in test log.

like image 279
Shuo Avatar asked Mar 10 '13 23:03

Shuo


People also ask

How do you know if greater than assert?

The assertGreaterThan() function is a builtin function in PHPUnit and is used to assert whether the actual value is greater than the expected value or not.

What does assertArrayEquals mean in JUnit?

assertArrayEquals. public static void assertArrayEquals(String message, Object[] expecteds, Object[] actuals) throws org.junit.internal.ArrayComparisonFailure. Asserts that two object arrays are equal. If they are not, an AssertionError is thrown with the given message.

What does assertAll do in JUnit?

assertAll. One of the new assertion introduced in JUnit 5 is assertAll. This assertion allows the creation of grouped assertions, where all the assertions are executed and their failures are reported together.

What is assertThat in JUnit?

The assertThat is one of the JUnit methods from the Assert object that can be used to check if a specific value match to an expected one. It primarily accepts 2 parameters. First one if the actual value and the second is a matcher object.


2 Answers

You can import Hamcrest like this and use the Matchers.lessThan() method.

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

assertThat(foo, Matchers.lessThan(bar));
like image 68
user2601995 Avatar answered Sep 17 '22 15:09

user2601995


Have you tried JUnit + Hamcrest? See this blog post for some examples—it looks almost exactly like what you posted:

JUnit 4 Showcase – assertThat and Hamcrest Matchers

Alternatively, there's also ComparableAssert from the JUnit-addons project.

like image 38
DaoWen Avatar answered Sep 20 '22 15:09

DaoWen