Skip to content

A java test utilities library, it supports assertion, random, performance and reflections test

License

Notifications You must be signed in to change notification settings

tvd12/test-util

Repository files navigation

Build Status Dependency Status Coverage Status Maven Central Javadocs

Synopsis

This project support for:

  • testing private, protected, package access and public method
  • testing script performance

Documentation

https://youngmonkeys.org/projects/test-utilities

Code Example

1. Test script performance

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

2. Assertion

Asserts.assertEquals(expected, actual);

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

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

3. Random

RandomUtil.randomSmallInt();

RandomUtil.randomShortAlphabetString();

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

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

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

Motivation

Because sometimes we want to call private, protected, package access and public method, we need test performance to our script and export result to file so, we need create a library to support them

Installation

Maven

<dependency>
	<groupId>com.tvd12</groupId>
	<artifactId>test-util</artifactId>
	<version>1.1.7</version>
</dependency>

You need create file AllTests.tng.xml in your src/test/resources folder with content, example:

<!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>

Gradle

testImplementation 'com.tvd12:test-util:1.1.7'

API Reference

http://www.javadoc.io/doc/com.tvd12/test-util

Tests

mvn test

Contact us

License

  • Apache License, Version 2.0