Skip to content

Commit 865f1f6

Browse files
committed
Introduce tests for @SQL and @transactional support in @nested test classes
This commit was inspired by the following question on Stack Overflow. https://stackoverflow.com/questions/53236807/spring-boot-testing-run-script-in-a-nested-test-sql-script-sql
1 parent 93df24a commit 865f1f6

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

spring-test/src/test/java/org/springframework/test/context/jdbc/PopulatedSchemaDatabaseConfig.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.jdbc.core.JdbcTemplate;
2324
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
2425
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
2526
import org.springframework.transaction.PlatformTransactionManager;
@@ -47,4 +48,9 @@ public DataSource dataSource() {
4748
.build();
4849
}
4950

51+
@Bean
52+
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
53+
return new JdbcTemplate(dataSource);
54+
}
55+
5056
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit.jupiter.nested;
18+
19+
import org.junit.jupiter.api.Nested;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.TestInstance;
22+
import org.junit.jupiter.api.TestInstance.Lifecycle;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.jdbc.core.JdbcTemplate;
26+
import org.springframework.test.context.jdbc.PopulatedSchemaDatabaseConfig;
27+
import org.springframework.test.context.jdbc.Sql;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
29+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
30+
import org.springframework.test.context.transaction.AfterTransaction;
31+
import org.springframework.test.context.transaction.BeforeTransaction;
32+
import org.springframework.test.jdbc.JdbcTestUtils;
33+
import org.springframework.transaction.annotation.Transactional;
34+
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
36+
37+
/**
38+
* Integration tests that verify support for {@link Nested @Nested} test classes in
39+
* conjunction with the {@link SpringExtension}, {@link Sql @Sql}, and
40+
* {@link Transactional @Transactional} in a JUnit Jupiter environment.
41+
*
42+
* @author Sam Brannen
43+
* @since 5.1.2
44+
*/
45+
@SpringJUnitConfig(PopulatedSchemaDatabaseConfig.class)
46+
@Transactional
47+
@TestInstance(Lifecycle.PER_CLASS)
48+
class NestedTestsWithSqlScriptsAndJUnitJupiterTests {
49+
50+
@Autowired
51+
JdbcTemplate jdbcTemplate;
52+
53+
@BeforeTransaction
54+
@AfterTransaction
55+
void checkInitialDatabaseState() {
56+
assertEquals(0, countRowsInTable("user"));
57+
}
58+
59+
@Test
60+
@Sql("/org/springframework/test/context/jdbc/data.sql")
61+
void sqlScripts() {
62+
assertEquals(1, countRowsInTable("user"));
63+
}
64+
65+
private int countRowsInTable(String tableName) {
66+
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
67+
}
68+
69+
@Nested
70+
@SpringJUnitConfig(PopulatedSchemaDatabaseConfig.class)
71+
@Transactional
72+
class NestedTests {
73+
74+
@Autowired
75+
JdbcTemplate jdbcTemplate;
76+
77+
@BeforeTransaction
78+
@AfterTransaction
79+
void checkInitialDatabaseState() {
80+
assertEquals(0, countRowsInTable("user"));
81+
}
82+
83+
@Test
84+
@Sql("/org/springframework/test/context/jdbc/data.sql")
85+
void nestedSqlScripts() {
86+
assertEquals(1, countRowsInTable("user"));
87+
}
88+
89+
private int countRowsInTable(String tableName) {
90+
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
91+
}
92+
93+
}
94+
95+
}

0 commit comments

Comments
 (0)