使用@DataJpaTest时未配置JdbcTemplate

时间:2016-09-01 06:42:45

标签: java spring unit-testing spring-boot jdbctemplate

我使用@DataJpaTest注释创建了一个测试。 hsqldb已配置,但我收到错误:

No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate]

在项目中,我使用的是JPA存储库和JdbcTemplates。当我使用真正的Oracle数据库配置时,它工作正常。为什么没有自动配置JdbcTemplate?我该怎么做才能解决这个问题?

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApplication.class})
@DataJpaTest
public class IntegrationTest

依赖关系:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>2.2.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>args4j</groupId>
    <artifactId>args4j</artifactId>
    <version>2.33</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

<dependency>
    <groupId>oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3.0</version>
</dependency>

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.4.18</version>
</dependency>
<dependency>
    <groupId>org.perf4j</groupId>
    <artifactId>perf4j</artifactId>
    <version>0.9.16</version>
</dependency>
<dependency>
    <groupId>io.reactivex</groupId>
    <artifactId>rxjava</artifactId>
    <version>1.0.13</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.2.3.RELEASE</version>
</dependency>

属性:

spring.jpa.database=ORACLE
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=validate

spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=..
spring.datasource.username=..
spring.datasource.password=..

spring.datasource.max-active=20
spring.datasource.max-idle=5
spring.datasource.min-idle=1
spring.datasource.initial-size=5


spring.datasource.testWhileIdle = true
spring.datasource.timeBetweenEvictionRunsMillis = 20000
#spring.datasource.test-on-borrow=true
spring.datasource.validation-query=select 1 from dual;

spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_size=100
spring.jpa.properties.hibernate.cache.use_second_level_cache=false

2 个答案:

答案 0 :(得分:3)

目前尚未配置。我already answered how to create your own slice并且我最近写了a blog post about it

没有特别原因JdbcTemplate未配置DataJpaTest。该注释的主要用例是测试 JPA 数据层。当你这样做时,你可能不应该使用JdbcTemplate,但我知道你可能会在测试中改变数据库或其他东西吗?

我已创建#6802来跟踪您的请求,请按此处进行更新。

答案 1 :(得分:1)

我只需将此文件添加到测试范围包中即可解决我的问题

@Configuration
public class TestConfig {
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}