General Examples
Generated imports
When creating a test template, Symflower automatically adds all required imports to the generated test file.
The following example code relies on a class Triangle
which resides in another package.
package com.symflower.example;
import com.symflower.shapes.Triangle;
import com.symflower.shapes.TriangleType;
public class Utils {
Triangle initTriangle() {...}
...
}
The generated test template file looks as follows. Note that the necessary import for Triangle
and the required dependencies for unit testing with JUnit 5 have been added automatically.
- JUnit 4
- JUnit 5
package com.symflower.example;
import org.junit.*;
import static org.junit.Assert.*;
import com.symflower.shapes.TriangleType;
import com.symflower.shapes.Triangle;
public class UtilsTest {
...
}
package com.symflower.example;
import com.symflower.shapes.Triangle;
import com.symflower.shapes.TriangleType;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class UtilsTest {
...
}
Template values
Symflower's test templates initialize variables with a predefined value. The following value lists the default value used per type.
Type | Template Value |
---|---|
byte | 123 |
short | 123 |
int | 123 |
long | 123L |
float | 123.4F |
double | 123.4D |
String | "abc" |
char | 'a' |
boolean | true |
For complex data types, Symflower's test templates aim to choose the simplest constructor for initialization. For enums, the template value is set to the first encountered enumeration constant.
Let's take a look at a concrete example to see how variables are typically initialized with Symflower's test templates! The method under test initTriangle
receives a TriangleType
and returns an initialized Triangle
:
package com.symflower.example;
import com.symflower.shapes.Triangle;
import com.symflower.shapes.TriangleType;
public class Utils {
static Triangle initTriangle(TriangleType t) {...}
}
The generated test template then looks as follows:
- JUnit 4
- JUnit 5
package com.symflower.example;
import org.junit.*;
import static org.junit.Assert.*;
import com.symflower.shapes.TriangleType;
import com.symflower.shapes.Triangle;
public class UtilsTest {
@Test
public void initTriangle() {
TriangleType t = TriangleType.equilateral;
Triangle expected = new Triangle(123, 123, 123);
Triangle actual = Utils.initTriangle(t);
assertEquals(expected, actual);
}
}
package com.symflower.example;
import com.symflower.shapes.Triangle;
import com.symflower.shapes.TriangleType;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class UtilsTest {
@Test
public void initTriangle() {
TriangleType t = TriangleType.equilateral;
Triangle expected = new Triangle(123, 123, 123);
Triangle actual = Utils.initTriangle(t);
assertEquals(expected, actual);
}
}
Note that the required enum t
is initialized with the enum value TriangleType.equilateral
and the expected Triangle
by calling the only provided constructor new Triangle(123, 123, 123)
.
Assertions
The generated assertions depend on the recommended assertion function per type. Most often, assertEquals
is used to compare the results of a function. In the case of a returned array type, the function assertArrayEquals
is used instead:
package com.symflower.example;
import com.symflower.shapes.Triangle;
import com.symflower.shapes.TriangleType;
public class Utils {
...
static Triangle[] getTriangles(){
...
}
}
Here’s what the generated test looks like:
- JUnit 4
- JUnit 5
package com.symflower.example;
import com.symflower.shapes.Triangle;
import org.junit.*;
import static org.junit.Assert.*;
public class UtilsTest {
@Test
public void getTriangles() {
Triangle[] expected = { new Triangle(123, 123, 123), new Triangle(123, 123, 123), new Triangle(123, 123, 123) };
Triangle[] actual = Utils.getTriangles();
assertArrayEquals(expected, actual);
}
}
package com.symflower.example;
import com.symflower.shapes.Triangle;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class UtilsTest {
@Test
public void getTriangles() {
Triangle[] expected = { new Triangle(123, 123, 123), new Triangle(123, 123, 123), new Triangle(123, 123, 123) };
Triangle[] actual = Utils.getTriangles();
assertArrayEquals(expected, actual);
}
}