Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a fake javax.sql.DataSource?

I have to write tests with String Test framework where many DB connections was used.

In test I don't need all data sources but Spring want them all to inject.

Are there any standard or well known fake javax.sql.DataSource implementation just to satisfy Spring DI mechanic?

like image 896
gavenkoa Avatar asked Jun 18 '14 16:06

gavenkoa


2 Answers

Using Mockito (or other mocking framwork) is preferred (as pointed out in other answers). However if you just want to make your application context up (without complaining about data source), you can use org.springframework.jdbc.datasource.SimpleDriverDataSource.

You need to provide bean to the context with the name which overrides the original bean and make sure your fake bean is scanned first.

@Configuration
public class MyFakeConfig {

  @Bean(name = "NAME OF THE ORIGINAL BEAN TO OVERRIDE")
  public DataSource fakeDataSource() {
    return new SimpleDriverDataSource()

  }
}
like image 117
walkeros Avatar answered Sep 28 '22 04:09

walkeros


You can use Mockito framework. Using Springockito you can mock your datasources on a Spring environment.

Credit of this resource is for kubek2k in this SO answer.

like image 25
Spacemonkey Avatar answered Sep 28 '22 04:09

Spacemonkey