A minimalist Java library for effortless unit test execution. Although functional, my main goal was to put my studies on annotation and reflection in Java into practice. It was also a fun challenge to develop a test library from scratch and without references.
UVATester's main process can be summarized as follows:
- Receiving the name of a test class, loading it dynamically via reflection.
- Checking that the class has the
@TestClass
annotation. - Invoking each method in the class that has the
@TestMethod
annotation and follows certain criteria (for more details, see the "Mapping process" section). - If any problems occur during the execution of the tests, the details of the error will be logged and then the UVATester will be terminated.
Mandatory:
- Java 17+
- Maven 4.0.0+
Optional:
- JitPack
Add and download the following dependencies to your pom.xml
:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.foiovituh</groupId>
<artifactId>uva-tester</artifactId>
<version>v1.0-SNAPSHOT</version>
</dependency>
</dependencies>
Annotate your test class with @TestClass
, and annotate your test methods with @TestMethod
. The test methods should be static, return void, and have no arguments. For example:
@TestClass
public class ShoppingTrolleyTest {
@TestMethod
public static void testEmptyShoppingTrolleyGuardingReturnsTrue() {
final var shoppingTrolley = new ShoppingTrolley();
final var testName = "testEmptyShoppingTrolleyGuardingReturnsTrue";
final var testResult = shoppingTrolley.guardShoppingTrolley()
&& Assertor.isNullOrEmpty(shoppingTrolley.getItems())
? "pass"
: "failed";
Feedback.showResult(testName, testResult);
}
}
Instantiate a UVATester
object using the test class name and invoke its run()
method to execute all defined tests:
public class ShoppingTrolleyTestExecutor {
public static void main(String[] args) {
final var shoppingTrolleyTests = new UVATester(
ShoppingTrolleyTest.class.getName()
);
shoppingTrolleyTests.run();
}
}
You can also use assertion methods that can be useful when developing tests. They are available in the class: com.github.foiovituh.uvatester.utils.Assertor
. For example, the method:
public static boolean isWithinRange(int value, int min, int max) {
return value >= min && value <= max;
}
- Add more methods to the Assertor class
- Make it optional to close tests if one fails
If you have any ideas or wish to contribute to the project, contact me on X (@ohtoaki) or send me a pull request :)
Distributed under the MIT License. See LICENSE
for more information.