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.
Steps to use StackSpot AI to generate tests and ensure 100% mutation coverage
-
Inside the Message.java class, select the code, right-click to open the menu, and select "StackSpot AI > Add tests for this code."
-
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.
- 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.
Pitest report from the Message.j
Example 2
-
Open a project, in this example is
Calc.java
class. -
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
- After a few seconds, StackSpot AI generates the test class with all scenarios and code explanations.
- 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));
}
}
- Rerun the tests. The report indicates that all scenarios have achieved code 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
-
For more details, please refer to the article on the StackSpot Blog.
-
Discover more about how StackSpot AI can help create unit tests in this video: