Introduce Test Utilities

test-util is an utilities library for unit test. It provide some features:
  1. Performance test
  2. Assertion
  3. Random data
  4. Reflection call

1. Installation

You can add to your dependecies like this:
Maven
<dependency>
    <groupId>com.tvd12</groupId>
    <artifactId>test-util</artifactId>
    <version>1.1.7</version>
</dependency>
Gradle
testImplementation 'com.tvd12:test-util:1.1.7'
You can find the latest version here.
By default, test-util is using testng, so if you don't want to use testng you can exclude it like this:
<dependency>
    <groupId>com.tvd12</groupId>
    <artifactId>test-util</artifactId>
    <version>${test.util.version}</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
        </exclusion>
    </exclusions>
</dependency>
If you still want to use testng and you get an error, maybe you will need create file AllTests.tng.xml in the folder src/test/resources like this:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All-Test">
    <test name="All">
        <packages>
            <package name="your_test_package"/>
        </packages>
    </test>
</suite>

2. Performace test

You can put test script, number of thread count (default is 0), number of loop count (default is 1.000.000) and then test-util will run and return the elapsed time for you, example:
long time = Performance.create()
    .threadCount(100) // set 0 if you want to run in sync mode
    .loop(1000000000) // optional, default 1000000
    .test(() -> System.out.println("Hello World"))
    .getTime();

3. Assertion

Assertion is very important in the unit test, it will help you verify whether your test output data is corrent or not, example:
Asserts.assertEquals(expected, actual);

Asserts.assertThat(actual).isEqualsTo(expected);

Asserts.assertThat(future).isEqualsTo(expected);

4. Random

Randomized data is a good idea for unit test, you will not have a headache about what input value you should put to a test case. And in some situation a randomized will help you detect a random bug. You can random a value like this:
RandomUtil.randomSmallInt();

RandomUtil.randomShortAlphabetString();

RandomUtil.randomMap(size, int.class, String.class);

5. Reflection call

Sometimes, your function you want to test is not public and you need use reflection, test-util will help you like this:
Get method by name
// with no arguments
Method nothing = MethodUtil.getMethod("nothing", ClassA.class);

// with one argument (Integer)
Method add = MethodUtil.getMethod("add", ClassA.class, Integer.class);
Invoke method
// invoke method
Integer result = MethodUtil.invokeMethod(add, new ClassA(), new Integer(1));

//invoke method by name
Integer result = MethodUtil.invokeMethod("add", new ClassA(), new Integer(1));

// invoke static method by name
MethodUtil.invokeStaticMethod("hello", ClassA.class, "tvd12.com");

// use builder syntax
Integer result = MethodInvoker.create()
        .method("add")
        .param(new Integer(1))
        .object(new ClassA())
        .invoke(Integer.class);
Get field value
GoodByeService goodByeService = FieldUtil.getFieldValue(goodbyeController, "goodByeService");
Set field value (even the field is final)
FieldUtil.setFieldValue(goodbyeController, "goodByeService", goodByeService);