java - 使用@DataJpaTest 时未配置 JdbcTemplate

标签 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

最佳答案

目前还没有配置。我有 already answered how to create your own slice我最近写了a blog post about it .

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

我创建了 #6802要跟踪您的请求,请关注那里以获取更新。

关于java - 使用@DataJpaTest 时未配置 JdbcTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39264354/

相关文章:

java - 为什么需要 ListModel 对象?

javascript - 监视 Jasmine 中另一个函数调用的函数

unit-testing - 单元测试在构建服务器上执行缓慢

node.js - mocha 单元测试返回 0 作为未捕获异常的退出代码

java - 使用Java搜索ms-word文件的内容

java - Libgdx - 仅动态着色 Sprite 的一部分

java - 如何测试使用企业应用程序的 EJB 模块

Spring Boot 应用程序只接受 6 个请求,为什么?

spring - 按示例查询 Spring 数据

java - 如何调用logback.xml文件中的application.properties值?