JUnit Testing Is Easy

JUnit Testing can seem like a daunting task, but it's possible to break it down for ourselves.

What is JUnit Testing?

JUnit is the Java Unit Testing framework that allows the programmer to be able to run repeatable tests on various units within a given Java program.

The tests can be written with the expected result that a certain class or method should give and the programmer can test the accuracy of the program.

Let us look at how to run a JUnit test. For this example, we will use JUnit5 and IntelliJ.

Running JUnit Tests

1. First, it is necessary to create a Java project to run the tests on. At first, it is advisable that a simple project be created without the use of any frameworks.

We will look at the Add class.

public class AddNumbers {
public int a;
public int b;

public AddNumbers(int a, int b){
this.a = a;
this.b = b;

}

int getNumberA(){
return a;
}

int getNumberB(){
return a;
}

int addition(){
return a+b;
}

}
2. After typing the above program, click on the class name AddNumbers and press
Alt+Enter.
Alternatively, you can Right click -> Show Context Actions
3. From the drop-down box, select Create Test. This action will open the following dialog box;
If the testing library is not available as shown above, click the Fix button and the IDE will 
download the library.
4. Select JUnit5.
5. You can select any or all of the member methods of the class that you wish to test from the 
given check boxes.
Then, click OK.

6. Depending on the member methods you selected, the following Testing Class will be generated.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AddNumbersTest {

@Test
void getNumberA() {
}

@Test
void getNumberB() {
}
}

(This testing class includes only two of the member methods from the class that is being tested)

7. Now, it is possible to begin writing the code for the tests.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AddNumbersTest {

@Test
void getNumberA() {
AddNumbers newNumbers = new AddNumbers(3, 5);
assertEquals(3, newNumbers.getNumberA());
}

@Test
void getNumberB() {
AddNumbers newNumbers = new AddNumbers(3, 5);
assertEquals(5, newNumbers.getNumberB());
}
}

By running the above test, it is possible to note that there is an error in the code of getNumberB, since it gives the following error;

By this, it is possible to identify that a correction must be made.

Now, it is possible to see than in the code of the getNumberB method, the return value is a and not b.

This method can now be corrected to return the correct value: b.

Now, running the test class would give the following output;

Thus, it is possible to see how JUnit testing will aid in finding errors of code.

8. Now, let us try to test more inputs.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class AddNumbersTest {

@Test
void getNumberA() {
AddNumbers newNumbers = new AddNumbers(3, 5);
assertEquals(3, newNumbers.getNumberA());
assertEquals(5, newNumbers.getNumberA());
}

@Test
void getNumberB() {
AddNumbers newNumbers = new AddNumbers(3, 5);
assertEquals(5, newNumbers.getNumberB());
assertEquals(3, newNumbers.getNumberB());
}
}

This test will also return as a failed test as it does not match the required logic.

9. Running tests with Coverage

This means that the IDE gives statistics of how much of the program has been tested. In order to do this,

Right click the green play button near the testing class name -> Run with Coverage (as shown below)

Alternatively, click the Run With Coverage button at the top right-hand corner of the IDE window.

This will run the tests and show the following display in the right-hand side of the IDE.

As shown, it is possible to see what percentage of each class, method and lines have been tested.

It shows that only 75% or 3/4 of the AddNumbers methods have been tested. 

This is due to the fact that the addition method declared above has not been tested.

In order to create a test for the missing method, the following steps can be followed.

10. Go to the AddNumbers.java class and place the cursor on the class name and follow the previous method of pressing Alt+Enter. This will display the following drop-down display.

Select the Generate missed test methods option.

This is display the following dialog box, from which you can select all the test methods that you wish to create and click OK.

11. Type the following test to test the addition class;

@Test
void addition() {
AddNumbers newNumbers = new AddNumbers(6, 8);
assertEquals(14, newNumbers.addition());
}

This test will give the following output.

12. Now, if you run the tests with coverage, you will get the following statistics.

The testing for the AddNumbers class has been completed.

Happy testing!

Comments

Popular Posts