Assertion
Assertion is very important in the unit test, it will help you verify whether your test output data is corrent or not, it's a main feature of test-util. There are 2 style of assertions:
assertXXX
and assertThat
. Basically, they're same you can choose any style that you prefer. 1. AssertXXX style
In almost cases, you usually use
equals
verification, test-util provides to you more than that: - Asserts.assertNull: verify actual value is null or not
- Asserts.assertNotNull: verify actual value is not null or null
- Asserts.assertThrows: verify and return and return a Throwable if the called method throws it
- Asserts.assertTrue: verify actual value is true of false
- Asserts.assertFalse: verify actual value is false or true
- Asserts.assertZero: verify actual number is zero or not
- Asserts.assertEmpty: verify actual collection is empty or not
- Asserts.assertEquals: verify actual value and expected value are equal or not. If you don't want compare types, you can use
Asserts.assertEquals(a, b, false)
- Asserts.assertNotEquals: verify actual value and expected value are not equal or equal
- Asserts.assertEqualsType: verify actual value type is equals expected type or not
Now, we can have an example. Let's say we have a class with
sum
method like this: public class Operation {
public int sum(int a, int b) {
return a + b;
}
}
We can test
equals
with random values like this: @Test
public void sumTest() {
// given
final int a = RandomUtil.randomSmallInt();
final int b = RandomUtil.randomSmallInt();
// when
final Operation operation = new Operation();
final int actual = operation.sum(a, b);
// then
final int expectation = a + b;
Assert.assertEquals(actual, expectation);
}
But, let's say we don't want to sum 2 negative numbers:
public class Operation {
public int sum(int a, int b) {
if (a < 0 || b < 0) {
throw new IllegalArgumentException("can not sum negative numbers");
}
return a + b;
}
}
Now, we can test exception case like this:
@Test
public void sumFailedDueToNegativeValuesTest() {
// given
final int a = RandomUtil.randomInt(-100, 0);
final int b = RandomUtil.randomSmallInt();
// when
final Operation operation = new Operation();
final Throwable e = Asserts.assertThrows(() ->
operation.sum(a, b)
);
// then
Asserts.assertEqualsType(e, IllegalArgumentException.class);
Asserts.assertEquals(e.getMessage(), "can not sum negative numbers");
}
2. AssertThat style
AssertThat style provides methods:
- AssertThat.isNull: verify actual value is null or not
- AssertThat.isNotNull: verify actual value is not null or null
- AssertThat.isTrue: verify actual value is true or false
- AssertThat.isFalse: verify actual value is false or true
- AssertThat.isZero: verify actual number is zero or not
- AssertThat.isEmpty: verify actual collection is empty or not
- AssertThat.test: test actual value
- AssertThat.isEqualsType: verify actual value type is equal expected type or not
-
AssertThat.isEqualsTo: verify actual value and expected value are equal or not. If you don't want compare types, you can use
isEqualsTo(a, b, false)
- AssertThat.willThrows: verify actual exception type
- AssertThat.acceptException: verify actual exception
- AssertThat.testException: verify actual exception
Back to
sum
method, we can test normal case with AssertThat style like this: @Test
public void sumTest() {
// given
final int a = RandomUtil.randomSmallInt();
final int b = RandomUtil.randomSmallInt();
// when
final Operation operation = new Operation();
final AssertThat<Integer> assertThat = Asserts.assertThat(() ->
operation.sum(a, b)
);
// then
final int expectation = a + b;
assertThat.isEqualsTo(expectation);
}
And we can test failure case with AssertThat style like this:
@Test
public void sumFailedDueToNegativeValuesTest() {
// given
final int a = RandomUtil.randomInt(-100, 0);
final int b = RandomUtil.randomSmallInt();
// when
final Operation operation = new Operation();
final AssertThat<Integer> assertThat = Asserts.assertThat(() ->
operation.sum(a, b)
);
// then
assertThat.testException(
e ->
e.getClass() == IllegalArgumentException.class &&
"can not sum negative numbers".equals(e.getMessage())
);
}