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?

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.

Best Java code snippets using javax.sql. DataSourceBuilder.create().build(); Smart code suggestions by Codota. } Connection connection = mock(​Connection.class); DataSource dataSource = mock(DataSource.class); given(​dataSource. A factory for XAConnection objects that is used internally. An object that implements the XADataSource interface is typically registered with a naming service that uses the Java Naming and Directory Interface TM (JNDI).

I do not know well known DataSource mock. You can mock it yourself using one of mock frameworks (e.g. Mockito) but IMHO better solution is to use pure java in-memory database like H2, HSQLDB or Derby. You will get real data source with real data that you can fill in test code programmatically and simulate any situation that can happen in your production code.

I ended up creating an example using the JDBC API that I think has a couple @​Mock. private DataSource ds;. @Mock. private Connection c;. 3 Make a fake javax.sql.DataSource? Jun 18 '14. 2 Class 'com.mchange.v2.c3p0.ComboPooledDataSource' not found Feb 14 '13. 2 Fetch dates (by month or year)

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.

public class MyFakeConfig {

  public DataSource fakeDataSource() {
    return new SimpleDriverDataSource()


This page provides Java code examples for javax.sql.DataSource. The examples are andReturn(null).once(); replay(mockConfig,mockDataSource); testClass. isClosed()) { DataSource ds=getDataSource(); if (ds != null) { connection=ds. Stack Overflow | The World’s Largest Online Community for Developers

You can use Spring's org.springframework.jdbc.datasource.AbstractDataSource which already provides some defaults for most of the interface methods. You only need to override two.

I ended up creating an example using the JDBC API that I think has a couple Assert; public class PersonDao { private DataSource ds; public Instead, we can use Mockito to create a “mock object” for each of these items. Introduction. A DataSource object provides a new way for JDBC clients to obtain a DBMS connection. To create a DataSource object you define it with an entry in the file. This DataSource entry then points to a connection pool that is also defined in the file.

This my sample which do job:

<!-- Create object with mocked parts that not used. -->
<bean id="config" class="org.Config">
        <bean class="org.mockito.Mockito" factory-method="mock">
            <constructor-arg value="javax.sql.DataSource"/>
    <property name="day" value="6"/>

Update pom.xml with:


This work with:


In teasing these things apart, we're able to build component code that is where a particular dependency was created will not care if that dependency is a fake (​mock) Configuration; import org.springframework.jdbc.datasource.embedded. Sets the log writer for this DataSource object to the given object.. The log writer is a character output stream to which all logging and tracing messages for this data source will be printed.

javax.sql.DataSource Scala Examples. The following examples show how to use javax.sql. getServiceManager.provide(classOf[SqlService]).get() val URL: String getString("driver") match { case "fake-db" => acc + (targetName -> new  DataSource (Java Platform SE 7 ) All Superinterfaces: CommonDataSource, Wrapper. public interface DataSource extends CommonDataSource, Wrapper. A factory for connections to the physical data source that this DataSource object represents. An alternative to the DriverManager facility, a DataSource object is the preferred means of getting a connection.

ifyou countJMS topics and queues as two different types), you must use one of the following: <res-type>javax.sql.DataSource</res-type> 1 <res-type>javax.jms. But you do need to know it's out there, ifyou need it. The complete resource mapping picture fake JNDI name: <resource-ref>CustDB</resource-ref> actual. An alternative to the DriverManager facility, a DataSource object is the preferred means of getting a connection. An object that implements the DataSource interface will typically be registered with a naming service based on the Java™ Naming and Directory (JNDI) API. The DataSource interface is implemented by a driver vendor. There are three

DataSourceBasedDBTestCase, uses a javax.sql.DataSource to create connections (with the aid of a DataSourceDatabaseTester). What is Disposable Temporary E-mail? Disposable email - is a service that allows to receive email at a temporary address that self-destructed after a certain time elapses. . It is also known by names like : tempmail, 10minutemail, throwaway email, fake-mail or trash-ma

  • The answer to this strongly depends on what the fake DataSources needs to be able to do.
  • @ThorbjørnRavnAndersen Nothing to do. Just to satisfy Spring DI engine with object. No calls to dataSource expected.
  • Make it a Provider<DataSource> instead then.
  • I afraid that mock(JdbcTemplate.class); can't be used in Spring .xml config, seems I have to migrate to Java config....
  • I found Springockito is a best approach as also can fake some other parts. +1
  • Thanks for suggestion! I thought about such solution but expected to find simple and non bloated solution.
  • It is simple. The first setup indeed will take an hour or two, but it will help you in future.