3

I created a test with @DataJpaTest annotation. hsqldb is configured but I'm getting error:

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

In the project I'm using JPA repositories and JdbcTemplates. It is working fine when I'm using real Oracle DB configuration. Why JdbcTemplate is not configured automatically ? What should I do in order to fix this problem ?

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

Dependencies:

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

Properties:

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
mrh
  • 917
  • 1
  • 9
  • 28
  • Property file contains oracle. Can you paste the configurations? – abaghel Sep 01 '16 at 06:53
  • Properties that I included are used in standard mode. I don't have anly other properties. I'm assuming that annotation @DataJpaTest is handling configuration of in memory DB. For now I only overwritten one property for testing: spring.jpa.hibernate.ddl-auto=none – mrh Sep 01 '16 at 07:04
  • Hmmm. Can you please show the complete error log? – abaghel Sep 01 '16 at 07:45

2 Answers2

3

It's not configured at the moment. I've already answered how to create your own slice and I've recently wrote a blog post about it.

There is no particular reason why JdbcTemplate isn't configured with DataJpaTest. The main use case for that annotation is to test your JPA data layer. When you do, you're probably not supposed to use a JdbcTemplate but I understand you may like that in your test to alter the database or something?

I've created #6802 to track your request, please follow there for updates.

Community
  • 1
  • 1
Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • There is another use case: test a DAO class which uses JdbcTemplate. That's not a JPA test, but in-memory database autoconfiguration is quite usefull for a JdbcTemplate DAO test too. – Guram Savinov Sep 26 '16 at 15:16
1

I was able to resolve my problem by simply adding this file into the test scoped package

@Configuration
public class TestConfig {
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
mrh
  • 917
  • 1
  • 9
  • 28