JUnit 5
Dependencies
JUnit 5 - At least
5.10.x
<dependency>
<groupId>guru.mikelue.foxglove</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.5</version>
<scope>test</scope>
</dependency>Extension
Package: guru.mikelue.foxglove.junit
Use FoxgloveJUnitExtension to enable Foxglove on JUnit 5 tests.
Annotations
Package: guru.mikelue.foxglove.junit.annotation
@TableFacetsSource - Annotation to provide TableFacet(s) for the test class.
@DataGeneratorSource - Annotation to provide DataGenerator(s) for the test class.
@GenData - Annotation to enable data generation for test methods or test class.
value- Array of TableFacetsProvider(s) defining the table facets.facets- Array of TableFacetsProvider(s) defining the table facets.facetsNames- Names of fields or methods, which are type of TableFacet,List<TableFacet>,Stream<TableFacet>or TableFacetsProvider.generator- Type ofDataGeneratorProviderproviding DataGenerator.generatorName- Name of DataGenerator.
Examples
Warning |
|
See JUnit 5 Examples for complete examples.
For @GenData - If none of properties provided, the extension will looking for @TableFacetsSource and @DataGeneratorSource on the test class.
FoxgloveJUnitExtensionimport guru.mikelue.foxglove.annotation.*;
import guru.mikelue.foxglove.jdbc.JdbcDataGenerator;
import guru.mikelue.foxglove.jdbc.JdbcTableFacet;
import guru.mikelue.foxglove.junit.FoxgloveJUnitExtension;
@ExtendWith(FoxgloveJUnitExtension.class)
public class JUnit5Test extends AbstractJdbcTestBase {
private final static int RANDOM_ROWS = gen().ints().range(5, 10).get();
public JUnit5Test() {}
/**
* For @GenData, the name will be same as the method name.
*/
@Test
@GenData(facetsNames = { "carsWithFeature" })
void junit5Method()
{
int testedCount = getJdbcTemplate().query(
"""
SELECT COUNT(DISTINCT cr_id)
FROM ap_car
INNER JOIN
ap_car_feature
ON cr_id = cf_cr_id
AND cf_feature_name = 'Sunroof'
""",
rs -> {
rs.next();
return rs.getInt(1);
}
);
assertThat(testedCount)
.isEqualTo(RANDOM_ROWS);
}
@TableFacetsSource
TableFacet[] carsWithFeature()
{
var carFacet = JdbcTableFacet.builder(TABLE_CAR)
.numberOfRows(RANDOM_ROWS)
.build();
return new TableFacet[] {
carFacet,
JdbcTableFacet.builder(TABLE_CAR_FEATURE)
.referencing("cf_cr_id")
.parent(carFacet, "cr_id")
.cardinality(2)
.column("cf_feature_name")
.roundRobin("Sunroof", "Leather Seats")
.build()
};
}
/**
* Default data generator for JDBC.
*/
@DataGeneratorSource
DataGenerator<?> defaultDataGenerator()
{
return new JdbcDataGenerator(getDataSource());
}
@BeforeEach
void setup() {}
@AfterEach
void tearDown()
{
deleteAll(TABLE_CAR);
}
}Functional utility types
Package: guru.mikelue.foxglove.junit.functional
TableFacetsProvider - A Supplier of
List<TableFacet>to provide table facets for the test class.TableFacetProvider - A Supplier of TableFacet to provide single table facet for the test class.
StreamOfTableFacetsProvider - A Supplier of
Stream<TableFacet>to provide table facets for the test class.