1. Project Clover database Wed Nov 12 2025 05:07:35 UTC
  2. Package guru.mikelue.foxglove.examples

File SpringFrameworkTest.java

 

Code metrics

0
20
16
3
241
197
16
0.8
1.25
5.33
1

Classes

Class Line # Actions
SpringFrameworkTest 42 15 0% 11 0
1.0100%
ToyotaCarsProvider 191 1 0% 1 0
1.0100%
SampleTestConfig 205 4 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    package guru.mikelue.foxglove.examples;
2   
3    import java.io.IOException;
4   
5    import org.springframework.beans.factory.annotation.Autowired;
6    import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
7    import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
8    import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
9    import org.springframework.context.ApplicationContext;
10    import org.springframework.context.annotation.Bean;
11    import org.springframework.context.annotation.Configuration;
12    import org.springframework.context.annotation.Scope;
13    import org.springframework.jdbc.core.JdbcTemplate;
14    import org.springframework.test.context.ContextConfiguration;
15    import org.springframework.test.jdbc.JdbcTestUtils;
16    import org.springframework.transaction.annotation.Propagation;
17    import org.springframework.transaction.annotation.Transactional;
18    import org.springframework.util.StreamUtils;
19   
20    import org.instancio.Instancio;
21    import org.junit.jupiter.api.AfterEach;
22    import org.junit.jupiter.api.BeforeAll;
23    import org.junit.jupiter.api.BeforeEach;
24    import org.junit.jupiter.api.Test;
25   
26    import guru.mikelue.foxglove.TableFacet;
27    import guru.mikelue.foxglove.annotation.GenData;
28    import guru.mikelue.foxglove.annotation.TableFacetsSource;
29    import guru.mikelue.foxglove.functional.TableFacetProvider;
30    import guru.mikelue.foxglove.jdbc.JdbcTableFacet;
31    import guru.mikelue.foxglove.springframework.EnableFoxglove;
32   
33    import static guru.mikelue.foxglove.test.SampleSchema.*;
34    import static java.nio.charset.StandardCharsets.UTF_8;
35    import static org.assertj.core.api.Assertions.assertThat;
36   
37    // tag::springframworkTest[]
38    @JdbcTest
39    @AutoConfigureTestDatabase(replace = Replace.NONE)
40    @ContextConfiguration(classes = SampleTestConfig.class)
41    @EnableFoxglove
 
42    public class SpringFrameworkTest {
 
43  4 toggle public SpringFrameworkTest() {}
44   
45    @TableFacetsSource
46    TableFacet redCars = JdbcTableFacet.builder("ap_car")
47    .numberOfRows(RANDOM_ROWS)
48    .column("cr_color").fixed("red")
49    .build();
50   
51    /**
52    * The default transaction behavior of @JdbcTest is to rollback at the end of each test.
53    */
 
54  1 toggle @Test
55    @GenData(
56    facetsNames = { "redCars" }
57    )
58    void someTest(
59    @Autowired
60    JdbcTemplate jdbcTemplate
61    ) {
62  1 assertThat(
63    JdbcTestUtils.countRowsInTableWhere(
64    jdbcTemplate,
65    "ap_car", "cr_color = 'red'"
66    )
67    )
68    .isEqualTo(RANDOM_ROWS);
69    }
70    // end::springframworkTest[]
71   
72    final static int RANDOM_ROWS = Instancio.gen()
73    .ints().range(5, 10).get();
74   
75    // tag::useBeanOfSpringFramework[]
 
76  1 toggle @Test
77    @GenData(
78    // Uses the bean name of Spring Framework context
79    facetsNames = { "blueCars", "purpleCars", "yellowCars" }
80    )
81    void blueCarsTest(
82    @Autowired
83    JdbcTemplate jdbcTemplate
84    ) {
85  1 assertThat(
86    JdbcTestUtils.countRowsInTableWhere(
87    jdbcTemplate,
88    "ap_car", "cr_color IN ('blue', 'purple', 'yellow')"
89    )
90    )
91    .isEqualTo(RANDOM_ROWS * 3);
92    }
93    // end::useBeanOfSpringFramework[]
94   
95    // tag::noRollback[]
 
96  4 toggle @AfterEach
97    void tearDownForYellowCars(
98    @Autowired
99    JdbcTemplate jdbcTemplate
100    ) {
101    // You have to remove the data by yourself
102  4 JdbcTestUtils.deleteFromTableWhere(
103    jdbcTemplate,
104    "ap_car", "cr_color = 'yellow'"
105    );
106    }
107   
 
108  1 toggle @Test
109    @GenData(
110    facetsNames = { "yellowCars" }
111    )
112    @Transactional(propagation = Propagation.NOT_SUPPORTED)
113    void noRollback(
114    @Autowired
115    JdbcTemplate jdbcTemplate
116    ) {
117  1 assertThat(
118    JdbcTestUtils.countRowsInTableWhere(
119    jdbcTemplate,
120    "ap_car", "cr_color = 'yellow'"
121    )
122    )
123    .isEqualTo(RANDOM_ROWS);
124    }
125    // end::noRollback[]
126   
127    // tag::byProvider[]
 
128  1 toggle @Test
129    @GenData(ToyotaCarsProvider.class)
130    void byProvider(
131    @Autowired
132    JdbcTemplate jdbcTemplate
133    ) {
134  1 assertThat(
135    JdbcTestUtils.countRowsInTableWhere(
136    jdbcTemplate,
137    "ap_car", "cr_brand = 'Toyota'"
138    )
139    )
140    .isEqualTo(RANDOM_ROWS);
141    }
142    // end::byProvider[]
143   
 
144  4 toggle @BeforeEach
145    void setup() {}
146   
 
147  4 toggle @AfterEach
148    void tearDown() {}
149   
 
150  1 toggle @BeforeAll
151    static void globalSetup(ApplicationContext context) throws IOException
152    {
153  1 var jdbcTemplate = context.getBean(JdbcTemplate.class);
154   
155  1 dropTables(
156    jdbcTemplate,
157    TABLE_DATA_TYPES, TABLE_RENT, TABLE_CAR_FEATURE, TABLE_CAR, TABLE_MEMBER,
158    TABLE_CAR_ARCHIVED
159    );
160   
161  1 build("classpath:data-types.sql", context);
162  1 build("classpath:car-renting.sql", context);
163    }
164   
 
165  1 toggle private static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames)
166    {
167  1 for (var tableName: tableNames) {
168  6 jdbcTemplate.execute(
169    String.format("DROP TABLE IF EXISTS %s", tableName)
170    );
171    }
172    }
173   
 
174  2 toggle private static void build(String resourcePath, ApplicationContext appContext) throws IOException
175    {
176  2 var ddl = StreamUtils.copyToString(
177    appContext.getResource(resourcePath)
178    .getInputStream(),
179    UTF_8
180    );
181   
182  2 var jdbcTemplate = appContext.getBean(JdbcTemplate.class);
183   
184  2 for (var stmt: ddl.split(";")) {
185  11 jdbcTemplate.execute(stmt);
186    }
187    }
188    }
189   
190    // tag::providerClass[]
 
191    class ToyotaCarsProvider implements TableFacetProvider<TableFacet> {
 
192  1 toggle @Override
193    public TableFacet getOne()
194    {
195  1 return JdbcTableFacet.builder("ap_car")
196    .numberOfRows(SpringFrameworkTest.RANDOM_ROWS)
197    .column("cr_brand").fixed("Toyota")
198    .build();
199    }
200    }
201    // end::providerClass[]
202   
203    // tag::contextConfiguration[]
204    @Configuration
 
205    class SampleTestConfig {
 
206  1 toggle @Bean
207    ToyotaCarsProvider toyotaCars()
208    {
209  1 return new ToyotaCarsProvider();
210    }
211   
 
212  1 toggle @Bean @Scope("prototype")
213    TableFacet blueCars()
214    {
215  1 return JdbcTableFacet.builder("ap_car")
216    .numberOfRows(SpringFrameworkTest.RANDOM_ROWS)
217    .column("cr_color").fixed("blue")
218    .build();
219    }
220   
 
221  1 toggle @Bean
222    TableFacetProvider<TableFacet> yellowCars()
223    {
224  1 return () -> JdbcTableFacet.builder("ap_car")
225    .numberOfRows(SpringFrameworkTest.RANDOM_ROWS)
226    .column("cr_color").fixed("yellow")
227    .build();
228    }
229   
 
230  1 toggle @Bean @Scope("prototype")
231    TableFacet[] purpleCars()
232    {
233  1 return new TableFacet[] {
234    JdbcTableFacet.builder("ap_car")
235    .numberOfRows(SpringFrameworkTest.RANDOM_ROWS)
236    .column("cr_color").fixed("purple")
237    .build()
238    };
239    }
240    }
241    // end::contextConfiguration[]