spockito
Simple Java library to define data in a table-like manner. The library also provides a Junit 5 @TableSource annotation to define arguments for parameterized tests in a simple table structure. The SpockitoExtension can be used to automatically propagate fields in a test class with table data.
We also support parameterized test data in table format for Junit 4 tests through the classic Spockito test runner (see here for more information and examples for spockito with Junit 4).
Parameterized tests with @TableSource
Arguments for parameterized tests can be defined via @TableSource
annotation as follows:
public class TableSourceTest {
@TableSource({
"| Last Name | First Name |",
"| Jones | David |",
"| Jensen | Astrid |"
})
@ParameterizedTest(name = "[{index}] {1} {0}")
public void testUnrollNames(String lastName, String firstName) {
assertTrue(lastName.startsWith("J"), "Last Name should start with J");
assertTrue(firstName.endsWith("id"), "First Name should end with id");
}
@TableSource({
"| Name | Year | Birthday |",
"|-------|------|------------|",
"| Henry | 1981 | 1981-11-28 |",
"| Jessy | 1965 | 1965-03-28 |"
})
@ParameterizedTest(name = "[{index}] {0}")
public void testUnrollBirthdays(String name, int year, LocalDate birthday) {
assertEquals(5, name.length(), "Name should have 5 characters");
assertTrue(1990 > year, "Year is before 1990");
assertEquals(28, birthday.getDayOfMonth(), "Day is 28th");
assertEquals(year, birthday.getYear(), "Year is consistent with birthday");
}
@TableSource({
"| Name | Year | Birthday |",
"|-------|------|------------|",
"| Henry | 1981 | 1981-11-28 |",
"| Jessy | 1965 | 1965-03-28 |"
})
@ParameterizedTest(name = "[{index}] {0}")
public void testUnrollNameAndYearOnly(String name, int year) {
assertEquals(5, name.length(), "Name should have 5 characters");
assertTrue(1990 > year, "Year is before 1990");
}
@TableSource({
"| Object | Vertices | Angle sum |",
"|==========|==========|===========|",
"| Triangle | 3 | 180 |",
"| Square | 4 | 360 |",
"| Pentagon | 5 | 540 |",
"|----------|----------|-----------|",
})
@ParameterizedTest(name = "{2}: ({1}-2)*180 = {0}")
public void testUnrollAngularSums(@Column("Vertices") int n,
@Column("Angle sum") int degrees,
@Column("Object") String name) {
assertTrue(3 <= n, "There should be 3 or more vertices");
assertEquals(degrees, (n-2)*180, "Angular sum is wrong for: " + name);
}
}
This and other examples can be found here.
Run above test in IDE (here: IntelliJ)
This and other examples can be found here.
Maven
Add the following dependency to your maven pom.xml file:
<dependency>
<groupId>org.tools4j</groupId>
<artifactId>spockito-junit5</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
Download
Sources and binaries can be downloaded from maven central:
- tools4j-spockito in Maven Central