Introduce Test Utilities
Updated at 1689324588000- Performance test
 - Assertion
 - Random data
 - Reflection call
 
1. Installation
You can add to your dependencies like this:
Maven
<dependency> <groupId>com.tvd12</groupId> <artifactId>test-util</artifactId> <version>1.1.8</version> <scope>test</scope> </dependency>
Gradle
 testImplementation 'com.tvd12:test-util:1.1.8'
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>1.1.8</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:
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);