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?
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()
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With