1. What you will learn

In this guide you will learn how to write Unit Tests for simple Java Commands. This allows you to test the Command before shipping it to an application.

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 ant)

  • Read the Step by Step Simple Java Command guide

  • JUnit 5 environment

3. Dependencies

To write Unit Tests for a Command we need to have some dependencies included:

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

org.junit.jupiter:junit-jupiter

Allows the writing of JUnit5 tests

org.junit.jupiter:junit-jupiter-engine

4. The Unit Test

We want to test the Java Command we have written in the Step by Step Simple Java Command guide.

4.1. The Unit Test Base

In this chapter it is explained how the base structure of a Command Unit Test has to look like before even writing the first test. On the left side of the table below we have our simple Java Command, on the right we have their respective test class.

Command Class Test Class
@JavaCommand
public class Adding {

	@Persistent
	private int numberOne; (2)

	@Persistent
	private int numberTwo; (2)

	@Persistent(scope = FieldScope.WRITE_ONLY)
	private int result; (2)

	@Run
	public void run(){
	    this.result = this.numberOne + this.numberTwo;
	}
}
@ExtendWith(JlcintExtension.class) (1)
class AddingTest {

	@Persistent
	private int numberOne; (2)

	@Persistent
	private int numberTwo; (2)

	@Persistent
	private int result; (2)

	private Provider<Adding> command; (3)

}
1 To simulate flow of inputs and outputs of the Command, we need to extend our JUnit test with the JlcintExtension provided by the jlcint-testutils dependency.
2 The @Persistent fields of the Command class are mirrored inside the test class. This allows to assign inputs and read outputs during a test.
3 The Provider from the jlcint-testutils is parametized to the Command we want to test, in our case it is the Adding Command. The provider allows us to run the Command during a test with all their inputs set beforehand.
The @Persistent-Annotation of the Command Fields to not need do be mirrored 1 to 1. For the test class it is sufficient to have a simple annotation over the input and output fields.

4.2. The First Test

Now that we established a base, we can continue with writing our first test. We want to test if the Adding Command does truly add numbers according our expectations. Therefore, we write the following test.

@ExtendWith(JlcintExtension.class)
class AddingTest {

	@Persistent
	private int numberOne;
	@Persistent
	private int numberTwo;
	@Persistent
	private int result;

	private Provider<Adding> command;

        /**
	 * Tests if 1 + 2 = 3
	 */
	@Test (1)
	void test_adding() throws Exception{
		// Define inputs (2)
		this.numberOne = 1;
		this.numberTwo = 2;
		// Run Command (3)
		this.command.execute();
		// Verify outputs (4)
		assertEquals(3,this.result);
	}

}
1 We added the test method test_adding(), so we can test the command.
2 Before the Command is run, we need to set the inputs beforehand. The inputs for the Command are our test class fields numberOne and numberTwo accordingly.
3 After everything is set, we run the Command by calling execute() on the Provider field command.
4 When the Command runs without an exception we can verify their outputs. Here we assert that result does equal the expected result 3.

Now you can run the test and see if the Command runs as expected.

5. Summary

In this guide we have learned how to write a Unit Test for a simple Java Command. How the input and output fields can be accessed and how the Command can be run from within a test method.

6. What to do next?