1

I have the following dataSource defined in my spring-beans.xml file which I use in order to connect in my remote database :

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/sample"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>

</bean>

I am building a couple of jUnit integration tests that I want to run. Some of the functions that get called from these tests use this datasource in order to access my database. When I deploy my project the dataSource is injected according to beans configuration that I have done.

For these tests that will run independently of the web application how can I inject this dataSource in order to access the database ?

I use the SpringFramework 2.5.6 version and jUnit4 for my tests.

1
  • 1
    "SpringFramework 2.5.6" that's a pretty old version.. Commented Jan 5, 2014 at 16:09

1 Answer 1

2

Integration test with Spring

Sample jUnit Integration test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "spring-beans.xml")
public class MyIntegrationTest {

   @Autowired
   DataSource dataSource;

}

Read more in Spring Framework Reference Documentation > Integration Testing > JDBC Testing Support

Database testing with Unitils

Unitils greatly reduces this complexity, making database testing easy and maintainable.

Unitils offers features for unit testing when working with Spring.

public abstract class BaseDAOTest extends UnitilsJUnit4 {

    @TestDataSource
    private DataSource dataSource;

    @Before    
    public void initializeDao() {
        BaseDAO dao = getDaoUnderTest();
        dao.setDataSource(dataSource);
    }

    protected abstract BaseDAO getDaoUnderTest();
}
Sign up to request clarification or add additional context in comments.

4 Comments

The dataSource is added correctly but when I execute the tests I get an : java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException ... To which class could this exception refer ?
This looks like classpath problem - stackoverflow.com/questions/9874369/…
Probably you have two version of junit on classpath, post new question with more information.
Well in my classpath I have only one version of junit , version 4.8.1 ... I read here stackoverflow.com/questions/12497857/… that the solution to this error is to downgrade to junit 4.4 but when I did this I got this error " java.io.FileNotFoundException: class path resource [tests/spring-beans.xml] cannot be opened because it does not exist" This is really strange because it is in the same place that I had it before (same folder with my test cases)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.