Commit 52d7282f authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent 65b4f61a
...@@ -5204,7 +5204,8 @@ Data JPA tests may also inject a ...@@ -5204,7 +5204,8 @@ Data JPA tests may also inject a
{sc-spring-boot-test-autoconfigure}/orm/jpa/TestEntityManager.{sc-ext}[`TestEntityManager`] {sc-spring-boot-test-autoconfigure}/orm/jpa/TestEntityManager.{sc-ext}[`TestEntityManager`]
bean which provides an alternative to the standard JPA `EntityManager` specifically bean which provides an alternative to the standard JPA `EntityManager` specifically
designed for tests. If you want to use `TestEntityManager` outside of `@DataJpaTests` you 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] [source,java,indent=0]
---- ----
......
...@@ -8,6 +8,7 @@ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration ...@@ -8,6 +8,7 @@ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration
......
...@@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -49,6 +50,9 @@ public class DataJpaTestIntegrationTests { ...@@ -49,6 +50,9 @@ public class DataJpaTestIntegrationTests {
@Autowired @Autowired
private TestEntityManager entities; private TestEntityManager entities;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired @Autowired
private ExampleRepository repository; private ExampleRepository repository;
...@@ -72,6 +76,9 @@ public class DataJpaTestIntegrationTests { ...@@ -72,6 +76,9 @@ public class DataJpaTestIntegrationTests {
Long id = this.entities.persistAndGetId(new ExampleEntity("spring", "123"), Long id = this.entities.persistAndGetId(new ExampleEntity("spring", "123"),
Long.class); Long.class);
assertThat(id).isNotNull(); 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 @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment