powered by ASAP

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
/**
 * @workflowDocu Simple command that resolves a given JSON path for the
 * given JSON string.
 */
@JavaCommand
@CommandGroup("org.example")
public class ResolveJsonPath {
	/**
     * @workflowDocu Service which provides the ability to resolve JSON paths
     */
	@LiteService (2)
	private JsonPathService jsonService;

	/**
     * @workflowDocu The JSON string to look in
     */
	@Persistent
	private String jsonString;

	/**
     * @workflowDocu The JSON path to search
     */
	@Persistent
	private String jsonPath;

	/**
     * @workflowDocu Result of the operation
     */
	@Persistent(scope = FieldScope.WRITE_ONLY)
	private String resolvedPath;

	@Run
	public void resolveJsonPath() {
		this.resolvedPath = this.jsonService.fetchJsonPath(this.jsonString, this.jsonPath);
	}
}
@ExtendWith({ MockitoExtension.class, JlcintExtension.class }) (1)
class ResolveJsonPathTest {
    // Service which provides the ability to resolve JSON paths
	@LiteService (2)
	private JsonPathService jsonService;

	// Service which provides JSON mapper functionality
    @InjectService (3)
	private IJsonMapper jsonMapper;

	// The JSON string to look in
	@Persistent
	private String jsonString;

	// The JSON path to search
	@Persistent
	private String jsonPath;

	//Result of the operation
	@Persistent(scope = FieldScope.WRITE_ONLY)
	private String resolvedPath;

	//Provides the ability to run the command without a client
	private Provider<ResolveJsonPath> command;
}
/**
* 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 the test 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
@ExtendWith({ MockitoExtension.class, JlcintExtension.class })
class ResolveJsonPathTest {
	// Service which provides the ability to resolve JSON paths
	@LiteService
	@Mock
	private JsonPathService jsonService;

	// The JSON string to look in
	@Persistent
	private String jsonString;

	// The JSON path to search
	@Persistent
	private String jsonPath;

	//Result of the operation
	@Persistent(scope = FieldScope.WRITE_ONLY)
	private String resolvedPath;

	//Provides the ability to run the command without a client
	private Provider<ResolveJsonPath> command;

	@BeforeEach
	void setAttributes() {
		// Set attributes
		this.jsonString = "{\"variable1\": \"value1\", \"variable2\": \"value2\", \"variable3\": \"value3\"}";
		this.jsonPath = "variable1";
	}

	@Test
	void runResolveJsonPathTestLiteService() {

		Mockito.when(this.jsonService.fetchJsonPath(this.jsonString, this.jsonPath)).thenReturn("value1");

		assertDoesNotThrow(() -> this.command.execute());

		// Check the result of mocking test
		Mockito.verify(this.jsonService).fetchJsonPath(this.jsonString, this.jsonPath);
	}
}
@ExtendWith({ MockitoExtension.class, JlcintExtension.class })
class ResolveJsonPathServiceTest {
	// Service which provides the ability to resolve JSON paths
	@LiteService
	private JsonPathService jsonService;

	// Service which provides JSON mapper functionality
	@Mock
	@InjectService
	private IJsonMapper mockedJsonMapper;

	// The JSON string to look in
	@Persistent
	private String jsonString;

	// The JSON path to search
	@Persistent
	private String jsonPath;

	//Result of the operation
	@Persistent(scope = FieldScope.WRITE_ONLY)
	private String resolvedPath;

	//Provides the ability to run the command without a client
	private Provider<ResolveJsonPath> command;

	@BeforeEach
	void setAttributes() {
		// Set attributes
		this.jsonString = "{\"variable1\": \"value1\", \"variable2\": \"value2\", \"variable3\": \"value3\"}";
		this.jsonPath = "variable1";
	}

	@Test
	void runResolveJsonPathTestLiteService() {

		// Mock json Service (simple test implementation)
		this.jsonService = new JsonPathService();

		Mockito.when(this.mockedJsonMapper.resolvePathIn(this.jsonString, this.jsonPath.split("\\."))).thenReturn("value1");

		assertDoesNotThrow(() -> this.command.execute());

		// Check the result of mocking test
		Mockito.verify(this.mockedJsonMapper).resolvePathIn(this.jsonString, this.jsonPath.split("\\."));
	}
}
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.

5. Summary

In this guide we have learned how to write a UnitTest to run a Java Command using a LiteService with an InjectService within.