From 52d7282f5e4d979fc36e428e08ce0cb1d9bb43d8 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 2 Sep 2016 09:38:07 +0200 Subject: [PATCH] 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 --- .../src/main/asciidoc/spring-boot-features.adoc | 3 ++- .../src/main/resources/META-INF/spring.factories | 1 + .../autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 13fa1a572a..3d32d44ba3 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5204,7 +5204,8 @@ Data JPA tests may also inject a {sc-spring-boot-test-autoconfigure}/orm/jpa/TestEntityManager.{sc-ext}[`TestEntityManager`] bean which provides an alternative to the standard JPA `EntityManager` specifically designed for tests. If you want to use `TestEntityManager` outside of `@DataJpaTests` you -can also use the `@AutoConfigureTestEntityManager` annotation. +can also use the `@AutoConfigureTestEntityManager` annotation. A `JdbcTemplate` is also +available should you need that. [source,java,indent=0] ---- diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 9b71ea8486..eb62fa3f31 100644 --- a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -8,6 +8,7 @@ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ +org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java index d712f7d551..f94bb69dab 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestIntegrationTests.java @@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.context.ApplicationContext; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; @@ -49,6 +50,9 @@ public class DataJpaTestIntegrationTests { @Autowired private TestEntityManager entities; + @Autowired + private JdbcTemplate jdbcTemplate; + @Autowired private ExampleRepository repository; @@ -72,6 +76,9 @@ public class DataJpaTestIntegrationTests { Long id = this.entities.persistAndGetId(new ExampleEntity("spring", "123"), Long.class); assertThat(id).isNotNull(); + String reference = this.jdbcTemplate.queryForObject( + "SELECT REFERENCE FROM EXAMPLE_ENTITY WHERE ID = ?", new Object[] {id}, String.class); + assertThat(reference).isEqualTo("123"); } @Test