Using Symflower with JUnit 4 and JUnit 5
This tutorial shows you how to use Symflower to generate JUnit 4 and JUnit 5 test templates.
Out of the box, Symflower supports both JUnit 4 and JUnit 5. For projects using Maven and Gradle, Symflower will automatically detect the right framework to use. You can also manually configure Symflower to use either JUnit 4 or JUnit 5:
Tutorial project
This tutorial takes the example of a simple implementation of a class that stores triangles. Besides storing the length of each side of the triangle, the application should also categorize the triangle in question:
- Equilateral: all sides are the same length
- Isosceles: two sides have the same length
- Scalene: none of the sides are equal in length
Tutorial example
For this geometric triangle class, we'll need three member variables for the three triangle sides, a constructor, and the available triangle types. Let's also a possible implementation of the getType
function.
Copy the following code into a file called Triangle.java
:
public class Triangle {
private int sideA;
private int sideB;
private int sideC;
public Triangle(int sideA, int sideB, int sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
public TriangleType getType() {
// Each side needs to be greater than 0
if (sideA <= 0 || sideB <= 0 || sideC <= 0) {
return TriangleType.invalid;
}
// Two sides need to be bigger than the third
if(this.sideA + this.sideB < sideC || this.sideB + this.sideC < this.sideA || this.sideA + this.sideC < this.sideB){
return TriangleType.invalid;
}
if (this.sideA == this.sideB && this.sideB == this.sideC) {
return TriangleType.equilateral;
}
if (this.sideA == this.sideB || this.sideB == this.sideC || this.sideA == this.sideC) {
return TriangleType.isosceles;
}
return TriangleType.scalene;
}
}
enum TriangleType {
equilateral,
isosceles,
scalene,
invalid
}
Generate test template
Use any of the available usage options to generate smart test templates. A new test file TriangleTest.java
will be created with the following contents:
- JUnit 4
- JUnit 5
import org.junit.*;
import static org.junit.Assert.*;
public class TriangleTest {
@Test
public void Triangle() {
int sideA = 123;
int sideB = 123;
int sideC = 123;
Triangle expected = new Triangle(123, 123, 123);
Triangle actual = new Triangle(sideA, sideB, sideC);
assertEquals(expected, actual);
}
}
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class TriangleTest {
@Test
public void Triangle() {
int sideA = 123;
int sideB = 123;
int sideC = 123;
Triangle expected = new Triangle(123, 123, 123);
Triangle actual = new Triangle(sideA, sideB, sideC);
assertEquals(expected, actual);
}
}
Name test case and update values
Next up, choose an appropriate name for your test case and update the test values as you see fit. Your cursor will be automatically moved to the right location to assign a new name to your test case. Any subsequently generated smart test templates will be automatically added to the same test file. To move on, add the desired tests to your test file.