Skip to main content

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.

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.

TypeTemplate Value
byte123
short123
int123
long123L
float123.4F
double123.4D
String"abc"
char'a'
booleantrue

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:

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:

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);
}
}