Test Utilities Assertion

Updated at 1685528762000

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:

  1. Asserts.assertNull: verify actual value is null or not
  2. Asserts.assertNotNull: verify actual value is not null or null
  3. Asserts.assertThrows: verify and return and return a Throwable if the called method throws it
  4. Asserts.assertTrue: verify actual value is true of false
  5. Asserts.assertFalse: verify actual value is false or true
  6. Asserts.assertZero: verify actual number is zero or not
  7. Asserts.assertEmpty: verify actual collection is empty or not
  8. 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)
  9. Asserts.assertNotEquals: verify actual value and expected value are not equal or equal
  10. 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:

  1. AssertThat.isNull: verify actual value is null or not
  2. AssertThat.isNotNull: verify actual value is not null or null
  3. AssertThat.isTrue: verify actual value is true or false
  4. AssertThat.isFalse: verify actual value is false or true
  5. AssertThat.isZero: verify actual number is zero or not
  6. AssertThat.isEmpty: verify actual collection is empty or not
  7. AssertThat.test: test actual value
  8. AssertThat.isEqualsType: verify actual value type is equal expected type or not
  9. 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)
  10. AssertThat.willThrows: verify actual exception type
  11. AssertThat.acceptException: verify actual exception
  12. 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 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 assertThat = Asserts.assertThat(() ->
            operation.sum(a, b)
        );
        // then
        assertThat.testException(
            e ->
                e.getClass() == IllegalArgumentException.class &&
                "can not sum negative numbers".equals(e.getMessage())
        );
    }