0

It's veri similar to case [33213854]: However I have..

@SpringBootApplication
@EntityScan(basePackages= "com.bla.bla")
@Import({RepositoryRestMvcConfiguration.class, PersistenceContext.class})
public class Main {..}

@Configuration
// Using.. application.yml -------
@ConfigurationProperties(prefix = "spring.datasource") 
@EnableJpaRepositories(basePackages = {"com.aa.bb.repository"})
@EnableTransactionManagement 
public class PersistenceContext extends HikariConfig {

@Bean(destroyMethod = "close")
public DataSource dataSource() throws SQLException {
    return new HikariDataSource(this);
} ..
} ..

Hikari datasource is not being filled with the properties during the testing. The test is..

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {PersistenceContext.class}, 
initializers = {ConfigFileApplicationContextInitializer.class})
public class MyIntegrationTests {

@Autowired
MyRepository repository;

@Test
public void findsSomethingIn() {
    Page<ResourceBundle> bundles= this.repository.findAll(new PageRequest(0, 10));
    assertThat(bundles.getTotalElements(), is(greaterThan(20L)));
}
}

Application.yml

spring:
  datasource:
    driverClassName: org.postgresql.Driver
    jdbcUrl: jdbc:postgresql://localhost:5432/any
    username: uid
    password: xxx

However during the test I get..

IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required. at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:784)

I would really appreciate any advice.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jrr
  • 167
  • 1
  • 4
  • 14
  • 1
    `spring.datasource` is a namespace we already bind to something completely different so I'd avoid using that. Use a namespace of your own. I am confused as what you're trying to do: if you use the standard Spring Boot property, we auto-configure hikari for you already. Why are you defining things that are done automatically by boot? – Stephane Nicoll Jun 20 '16 at 06:21
  • Stephane, I supposed the same, everything it would be done by boot, that is not and I want to know why. Main.class is my boot, PersistenceContext.class is my config which should define other kind of AtBeans instead of Hikari datasource, but given that Hikari is not working with properties then I'm trying injecting them "manually".. and that is my concern.. why it's not working on my UnitTest?. So I'll use other namespace, however the documentation says: – Jrr Jun 20 '16 at 13:44
  • [link] (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-connect-to-production-database) Section: 29.1.2 Connection to a production database – Jrr Jun 20 '16 at 13:50
  • can you share a sample? I fail to see a reason why it wouldn't work in such a unit test. – Stephane Nicoll Jun 20 '16 at 14:14
  • I just want to run my unitTest.class using both boot-autoconfiguration and my custom-configuration class which would have BEANs except Hikari-datasource declaration, and to execute unitTest.class using them. Is this possible? (as aforementioned my unitTest.class is not working right now becuase spring-boot is not injecting the properties-values through autoconfiguration). Also I tried with myparams.datasource instead spring.datasource as prefix without success. – Jrr Jun 20 '16 at 20:09
  • Stephane, this is a concetrated information that I have faced on googling the trouble. (http://www.help-doing.com/sof/26490967.shtml), seems nobody have tried to use Hikari through boot-autoconfiguration.. isn't it? – Jrr Jun 20 '16 at 21:10

2 Answers2

0

SOLVED!!: Use @SpringApplicationConfiguration for Unit-TESTing instead of @ContextConfiguration since the test classes were not picking up the application configurations properly .. but WHY?. Reference Case: 22138366

Community
  • 1
  • 1
Jrr
  • 167
  • 1
  • 4
  • 14
0

For Spring 1.4, I had a very similar issue earlier, solved by adding: @EnableAutoConfiguration on to the configuration class (in my case it is DataSourceConfig.class)

And in the test, tag the test class as follow:

@RunWith(SpringRunner.class)
@Transactional()
@SpringBootTest(classes = DataSourceConfig.class)
@ActiveProfiles("dao-test")

-------- As an example --------

(1) My DataSourceConfig.class looks like:

@Configuration
@ComponentScan
@EnableTransactionManagement
@EnableAutoConfiguration
public class DataSourceConfig {

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

(2) And a test class look like this:

@RunWith(SpringRunner.class)
@Transactional()
@SpringBootTest(classes = DataSourceConfig.class)
@ActiveProfiles("dao-test")
public abstract class DaoTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    ...
}

(3) With pom file look like this:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

as wilkinsona mentioned: "If you want to use HikariCP, simply ensure that Tomcat's connection pool isn't on the classpath and that HikariCP is on the classpath. If you're using spring-boot-starter-jdbc you'll need to exclude tomcat-jdbc, otherwise simply adding a Hikari dependency should be sufficient."

see reference below.

(4) And resources/application-dao-test.properties look like this:

# PostgreSQL Data Source
spring.datasource.url=jdbc:postgresql://${IT_DOCKER_HOST_IP}:${IT_PG_PORT}/db-name
spring.datasource.username=xxx
spring.datasource.password=xxx
# the config below seems not needed:
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

Reference:

https://github.com/spring-projects/spring-boot/issues/1426 https://www.javacodegeeks.com/2016/03/springboot-working-jdbctemplate.html

Fuyang Liu
  • 1,496
  • 13
  • 26