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
pull/6851/head
Stephane Nicoll 8 years ago
parent 65b4f61a35
commit 52d7282f5e

@ -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]
----

@ -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

@ -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

Loading…
Cancel
Save