Skip to main content

Tutorial: Generate test suite (BETA)

To follow the feature tutorial below, start by copying the following code into a file named Copy.java:

class Copy {
static String[] copy(String[] from, String[] to) {
for (int i = 0; i < from.length; i++) {
to[i] = from[i];
}
return to;
}
}

Use the sample Copy.java code above and right-click anywhere in the function to open the context menu, then click "Symflower: Generate Test Suite for Function (BETA)". (Alternatively, use your defined hotkeys or the command palette to generate the test suite.) Symflower will analyze the source code and generate a file named CopySymflowerTest.java with the following test cases:

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class CopySymflowerTest {
@Test
public void copy1() {
String[] from = null;
String[] to = null;
// assertThrows(java.lang.NullPointerException.class, () -> {
Copy.copy(from, to);
// });
}
@Test
public void copy2() {
String[] from = {};
String[] to = null;
String[] actual = Copy.copy(from, to);
assertNull(actual);
}
@Test
public void copy3() {
String[] from = { null };
String[] to = null;
// assertThrows(java.lang.NullPointerException.class, () -> {
Copy.copy(from, to);
// });
}
@Test
public void copy4() {
String[] from = { null };
String[] to = { null };
String[] expected = { null };
String[] actual = Copy.copy(from, to);
assertArrayEquals(expected, actual);
}
@Test
public void copy5() {
String[] from = { "" };
String[] to = {};
// assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
Copy.copy(from, to);
// });
}
}

The above test cases include one test for each unique execution path of your code. Review Symflower-generated tests in the CopySymflowerTest.java file. Although those tests are already fully functional, you may want to update some values or update the test names to be more descriptive before adding them to your test suite.