SpringFramework

Dependencies

  • SpringFramework - At least 6.0.x

  • SpringBoot(Optional) - At least 3.1.x

    • It would be more convenient using SpringBoot for your tests.

test dependencies in pom.xml
<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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <version>3.1.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>3.1.12</version>
    <scope>test</scope>
</dependency>

Usage

Package: guru.mikelue.foxglove.springframework

Examples

  • JUnit 5 Examples for more examples of usage for @GenData, @DataGeneratorSource and @TableFacetsSource.

  • SpringFramwork Examples for more examples of application context and class-level sources for Foxglove.

    • SampleTestConfig is shown in this page

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.jdbc.JdbcTestUtils;

import guru.mikelue.foxglove.TableFacet;
import guru.mikelue.foxglove.annotation.GenData;
import guru.mikelue.foxglove.annotation.TableFacetsSource;
import guru.mikelue.foxglove.jdbc.JdbcTableFacet;
import guru.mikelue.foxglove.springframework.EnableFoxglove;

@JdbcTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
@ContextConfiguration(classes = SampleTestConfig.class)
@EnableFoxglove
public class SpringFrameworkTest {
    public SpringFrameworkTest() {}

    @TableFacetsSource
    TableFacet redCars = JdbcTableFacet.builder("ap_car")
        .numberOfRows(RANDOM_ROWS)
        .column("cr_color").fixed("red")
        .build();

    /**
     * The default transaction behavior of @JdbcTest is to rollback at the end of each test.
     */
    @Test
    @GenData(
        facetsNames = { "redCars" }
    )
    void someTest(
        @Autowired
        JdbcTemplate jdbcTemplate
    ) {
        assertThat(
            JdbcTestUtils.countRowsInTableWhere(
                jdbcTemplate,
                "ap_car", "cr_color = 'red'"
            )
        )
            .isEqualTo(RANDOM_ROWS);
    }
}