Skip to content

Commit 52d7282

Browse files
committed
Auto-configure JdbcTemplate with DataJpaTest
This commit adds `JdbcTemplateAutoConfiguration` to the list of auto- configurations that are applied with `DataJpaTest`. This effectively allows to inject a `JdbcTemplate` in any `@DataJpaTest` test. Closes gh-6802
1 parent 65b4f61 commit 52d7282

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5204,7 +5204,8 @@ Data JPA tests may also inject a
52045204
{sc-spring-boot-test-autoconfigure}/orm/jpa/TestEntityManager.{sc-ext}[`TestEntityManager`]
52055205
bean which provides an alternative to the standard JPA `EntityManager` specifically
52065206
designed for tests. If you want to use `TestEntityManager` outside of `@DataJpaTests` you
5207-
can also use the `@AutoConfigureTestEntityManager` annotation.
5207+
can also use the `@AutoConfigureTestEntityManager` annotation. A `JdbcTemplate` is also
5208+
available should you need that.
52085209

52095210
[source,java,indent=0]
52105211
----

spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration
88
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
99
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
1010
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
11+
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
1112
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
1213
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
1314
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration

spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
3030
import org.springframework.context.ApplicationContext;
31+
import org.springframework.jdbc.core.JdbcTemplate;
3132
import org.springframework.test.context.junit4.SpringRunner;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
@@ -49,6 +50,9 @@ public class DataJpaTestIntegrationTests {
4950
@Autowired
5051
private TestEntityManager entities;
5152

53+
@Autowired
54+
private JdbcTemplate jdbcTemplate;
55+
5256
@Autowired
5357
private ExampleRepository repository;
5458

@@ -72,6 +76,9 @@ public void testEntityManagerPersistAndGetId() throws Exception {
7276
Long id = this.entities.persistAndGetId(new ExampleEntity("spring", "123"),
7377
Long.class);
7478
assertThat(id).isNotNull();
79+
String reference = this.jdbcTemplate.queryForObject(
80+
"SELECT REFERENCE FROM EXAMPLE_ENTITY WHERE ID = ?", new Object[] {id}, String.class);
81+
assertThat(reference).isEqualTo("123");
7582
}
7683

7784
@Test

0 commit comments

Comments
 (0)