Skip to main content

Unit Tests

Unit tests ensure a program's code functions correctly by testing individual parts. They are crucial for maintaining software quality and easing code maintenance. However, writing unit tests can be time-consuming, so automating the process is essential.

Generate Unit Test: Example 1

This example demonstrates a project with tests for the Message.java class. When running the tests using Pitest to validate the Message.java, the results are as follows:

public class Message {
public String message(int a) {
String result;
if (a >= 0 && a <= 10) {
result = "YES";
} else {
result = "NO";
}
return result;
}
}

public class MessageTest {

@Test
public void messageOk1() {
Message message = new Message();
String result = message.message(5);
assertEquals("YES", result);
}

@Test
public void messageOk2() {
Message message = new Message();
String result = message.message(-5);
assertEquals("NO", result);
}
}

After running Pitest, the report shows that the Message.java class** has 100% line coverage but only 60% mutation coverage. This indicates that additional scenarios should be added to cover all possibilities.

Pitest report showing the line and mutation coverage of the Calc.java and Message.java classes. The Calc.java class has 0% line and mutation coverage. The Message.java class has 100% line coverage and 60% mutation coverage.

Pitest report from the Message.java class indicating that the limit analysis test is missing on line 6 of the code.

Steps to use StackSpot AI to generate tests and ensure 100% mutation coverage

  1. Inside the Message.java class, select the code, right-click to open the menu, and select "StackSpot AI > Add tests for this code."

  2. StackSpot AI processes the information and generates a class containing all the tests and a brief code explanation. You can add this class to your project or insert the code into an existing class.

Information generated by StackSpot AI displays a test class with all the test methods generated by the AI. Below the class is a brief explanation of the generated code.

  1. In this example, the existing tests have been replaced with those generated by StackSpot AI. The layout of the MessageTest.java class appears as follows:
public class MessageTest {

@Test
public void testMessageWithNumberInRange() {
Message message = new Message();
assertEquals("YES", message.message(5), "The message should be YES for numbers in range 0-10");
}

@Test
public void testMessageWithNumberOutOfRangePositive() {
Message message = new Message();
assertEquals("NO", message.message(11), "The message should be NO for numbers greater than 10");
}

@Test
public void testMessageWithNumberOutOfRangeNegative() {
Message message = new Message();
assertEquals("NO", message.message(-1), "The message should be NO for negative numbers");
}

@Test
public void testMessageWithBoundaryNumberZero() {
Message message = new Message();
assertEquals("YES", message.message(0), "The message should be YES for the boundary number 0");
}

@Test
public void testMessageWithBoundaryNumberTen() {
Message message = new Message();
assertEquals("YES", message.message(10), "The message should be YES for the boundary number 10");
}
}

After running the tests generated by StackSpot AI and checking the Pitest report, you can see that all test scenarios for the Message.java class have been covered.

The pitest report shows the line and mutation coverage of the Calc.java and Message.java classes. The Calc.java class has 0% line and mutation coverage, while the Message.java class has 100% line coverage and 100% mutation coverage.

Pitest report from the Message.j

Example 2

  1. Open a project, in this example is Calc.java class.

  2. Right-click to select the class code and choose "StackSpot AI > Add tests for this code" from the menu.

StackSpot AI menu displayed over the selected code

  1. After a few seconds, StackSpot AI generates the test class with all scenarios and code explanations.

Information generated by StackSpot AI, showing part of a test class with test methods generated by AI.

  1. StackSpot AI generates the CalcTest.java class, which will look like:
class CalcTest {

@Test
void testSomaWithValidNumbers() {
assertEquals(5, Calc.soma(2, 3));
}

@Test
void testSomaWithNulls() {
assertNull(Calc.soma(null, 3));
assertNull(Calc.soma(2, null));
assertNull(Calc.soma(null, null));
}

@Test
void testSubtraiWithValidNumbers() {
assertEquals(1, Calc.subtrai(4, 3));
}

@Test
void testSubtraiWithNulls() {
assertNull(Calc.subtrai(null, 3));
assertNull(Calc.subtrai(4, null));
assertNull(Calc.subtrai(null, null));
}

@Test
void testMultiplicaWithValidNumbers() {
assertEquals(12, Calc.multiplica(4, 3));
}

@Test
void testMultiplicaWithNulls() {
assertNull(Calc.multiplica(null, 3));
assertNull(Calc.multiplica(4, null));
assertNull(Calc.multiplica(null, null));
}

@Test
void testDivideWithValidNumbers() {
assertEquals(2, Calc.divide(6, 3));
}

@Test
void testDivideByZero() {
assertNull(Calc.divide(6, 0));
}

@Test
void testDivideWithNulls() {
assertNull(Calc.divide(null, 3));
assertNull(Calc.divide(6, null));
assertNull(Calc.divide(null, null));
}
}
  1. Rerun the tests. The report indicates that all scenarios have achieved code and mutation coverage.

This is a pitest report showing the line and mutation coverage of the Calc.java and Message.java classes. The Calc.java class has 100% line and mutation coverage, and the Message.java class has 100% line and mutation coverage.

Following these steps, you can effectively use StackSpot AI to generate comprehensive unit tests, ensuring your code achieves high coverage and quality.

Video & Blog