I try to change from application.properties
to application.yml
. My Spring Boot application starts and works fine, but all of integration test cases are failing.
This is error message at console log
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.10.RELEASE)
***************************
APPLICATION FAILED TO START
***************************
Description:
Field datasource in com.example.core.config.ServiceConfig
required a bean of type 'javax.sql.DataSource' that could not be found.
Action:
Consider defining a bean of type 'javax.sql.DataSource' in your configuration.
Although I defined datasource in file application.yml
and it works fine while running Spring boot app ... yet fails when running test cases, either via intelliJ runner or via Gradle -build task.
here are my configs:
spring:
profiles: default
datasource:
hikari:
minimum-idle: 1
maximum-pool-size: 5
pool-name: yourPoolName
auto-commit: false
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx
username: xxx
password: xxx
maximumPoolSize: 5
type: com.zaxxer.hikari.HikariDataSource
---
spring:
profiles: dev
datasource:
hikari:
minimum-idle: 1
maximum-pool-size: 5
pool-name: yourPoolName
auto-commit: false
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx
username: xx
password: xx
maximumPoolSize: 5
type: com.zaxxer.hikari.HikariDataSource
in my test class I tried everything :
@SpringBootTest also
@ContextConfiguration(classes = {ServiceConfig.class, ApplicationConfig.class},initializers = ConfigFileApplicationContextInitializer.class)
nothing seems to help.. runing test seems to ignore aapplication.yml completely
applicationConfig
@Configuration
public class ApplicationConfig {
/**
* Total customization - see below for explanation.
*/
@Autowired
private Environment environment;
@PostConstruct
private void inti () {
String [] ps = this.environment.getActiveProfiles();
for (String p : ps) {
System.out.println("Currrent profile is "+p);
}
}
@Autowired
DataSource datasource;
@Bean
public DataSourceTransactionManager transactionManager() throws SQLException {
return new DataSourceTransactionManager(datasource);
}
@Bean
public SqlSessionFactory mySqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(datasource);
return sessionFactory.getObject();
}
here is one of my test classes:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { ApplicationConfig.class})
@ActiveProfiles("dev")
public class OrdersServiceTest {
@Autowired
OXUserDetailsService ous;
@Autowired
OXOrderServices ors;
@Before
public void setup() throws Exception {
}
@Test
public void testgetMyOrders() {
OXUser ou = ous.getUserById(1);
OXCustomer c = ors.showOrderForUser(ou);
Assert.assertNotNull(c);
}
@Test
public void testgetOrderItemsByOrderId() {
List<OXOrderItem> ous =ors.getOrderItemsByOrderId(1);
Assert.assertNotNull(ous);
} }
spring version I use is 5.0.4 and below is relative boot dependencies have :
dependencies {
...
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version:'1.3'
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version:'2.2.0'
testCompile 'org.yaml:snakeyaml:1.19'
}
am I missing some spring boot magic config? why is it failing on test cases?
(1) File application.yml
, Add these lines
spring:
profiles: test
datasource:
hikari:
minimum-idle: 1
maximum-pool-size: 5
pool-name: yourPoolName
auto-commit: false
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx
username: xxx
password: xxx
maximumPoolSize: 5
type: com.zaxxer.hikari.HikariDataSource
By delcaring line profiles: test
, Spring Boot is auto-configuration for testing.
(2) File OrdersServiceTest.java
import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrdersServiceTest {
@Autowired
OXUserDetailsService ous;
@Autowired
OXOrderServices ors;
@Before
public void setup() throws Exception {
}
@Test
public void testgetMyOrders() {
OXUser ou = ous.getUserById(1);
OXCustomer c = ors.showOrderForUser(ou);
Assert.assertNotNull(c);
}
@Test
public void testgetOrderItemsByOrderId() {
List<OXOrderItem> ous = ors.getOrderItemsByOrderId(1);
Assert.assertNotNull(ous);
}
}
Reference: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing (supports auto-configuration for tests)
You might have extra-spaces in yml
file,
spring:
profiles: test
datasource:
hikari:
minimum-idle: 1
maximum-pool-size: 5
pool-name: yourPoolName
auto-commit: false
Would this work instead?
spring:
profiles: test
datasource:
hikari:
minimum-idle: 1
maximum-pool-size: 5
pool-name: yourPoolName
auto-commit: false
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.