UnitTest a Java Command with LiteService and InjectService
1. What You Will Learn
In this guide you will learn how to write UnitTests for Java Commands with LiteService
and
InjectService
. InjectService
allows you to implement a Service within another Service.
2. Prerequisites
To complete this guide you need:
-
Roughly 10 minutes
-
JDK11+ installed with JAVA_HOME configured appropriately
-
An IDE (we recommend IntelliJ)
-
Some kind of build tool (like Gradle, Maven or Apache Ant)
-
Read the UnitTest Java Command with Service guide
-
JUnit 5 environment
3. Dependencies
To write UnitTests for a Command we need to include some dependencies:
Dependencies | Reason to include |
---|---|
de.asap.pak.jlcint:jlcint-commandapi |
Provides annotations used to develop a Java Command |
de.asap.pak.jlcint:jlcint-testutils |
Utilities that allow the testing of Java Commands |
de.asap.pak.core:pak-context |
Contains the @InjectService annotation |
org.junit.jupiter:junit-jupiter |
Allows the writing of JUnit5 tests |
org.junit.jupiter:junit-jupiter-engine |
|
org.mockito:mockito-inline |
Allows the simulation of not implemented interfaces from other components by using mock objects instead |
org.mockito:mockito-junit-jupiter |
4. The UnitTest
We want to test the Java Command we have written in the PAK Service Based Command guide.
4.1. @LiteService
The @LiteService
annotation basically works like @Service
. A LiteService
is in fact a class that
implements the marker-interface ILiteService
. The respective class is mostly located in the same Java
module like the Command itself.
4.2. @InjectService
@InjectService
can be used for development inside the PAK-framework. Only Services registered within
IServiceProvider
, or the ServiceLoader
should be annotated by this annotation. Therefore, Services
from these instances can be injected into the Service, using @InjectService
, so they can access the
Services themselves easily.
4.3. UnitTest with LiteService and InjectService
Command Class | Test Class |
---|---|
|
|
/**
* Service class which provides JSON path functionality.
*/
public class JsonPathService implements ILiteService {
@InjectService (3)
private IJsonMapper jsonMapper;
/**
* Resolves a given JSON path in the given object using the Engine's json mapper.
*
* @param object Object to look in
* @param path Path to look for
* @return Resolved JSON path
*/
public String fetchJsonPath(final String object, final String path) {
return this.jsonMapper.resolvePathIn(object, path.split("\\."));
}
}
1 | Additional to the JlcintExtension the MockitoExtension which is provided by the mockito-junit-jupiter dependency is needed. The MockitoExtension provides the ability to use the @Mock annotation. |
2 | The Service under test is annotated with @LiteService and is usually an interface of an adapter. |
3 | The inner Service is annotated with @InjectService . |
The @Persistent -Annotation of the Command Fields need to be mirrored 1 to 1. For thetest class it is sufficient to have a simple annotation over the input and output fields. So @Persistent and for output fields the scope will be enough.
|
4.4. UnitTest with Mocked Services
To test the ResolveJsonPath
Command without the need to implement the LiteService
and InjectService
Services yourself, you can mock these services.
Keep in mind that this means that you have to test the functionality of your Services separately.
LiteService | InjectService |
---|---|
|
|
1 | With this Mockito Command the real Service object will be replaced by a mock object. |
2 | The mock object is checked if it is called correctly with the predefined attributes. |
Now you can run the test and see if the Command will be able to run in general.