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:
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:
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 "">
<suite name="All-Test">
    <test name="All">
            <package name="your_test_package"/>

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"))

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);



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.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, "");

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