4

I have such simple class for JUnit test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/mysql-datasource-context.xml"})
public class EmployeeDAOTest {

    @Autowired
    EmployeeDao employeeDao;

    @Test
    public void findAllTest() {
        assertTrue(employeeDao.findByName("noname").size() == 0);
    }
}

The content of the mysql-datasource-context.xml looks like this:

   <context:component-scan base-package="my.packages.*"/>

   <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/project"/>
          <property name="username" value="root"/>
          <property name="password" value="root"/>

   </bean>


   <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="packagesToScan" value="my.packages.entity"/>
          <property name="hibernateProperties">
                 <props>
                        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                        <prop key="show_sql">true</prop>
                        <prop key="hibernate.hbm2ddl.auto">update</prop>
                 </props>
          </property>
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"/>
   </bean>

   <tx:annotation-driven transaction-manager="transactionManager"/>

Now the test runs with no problem for my mysql database.

The point is that I also have a postgres database and I need every test run both for the mysql and postgres databases.

The only solution that comes to my mind is creating one more test class with exactly the same tests but annotate it as

@ContextConfiguration(locations = {"classpath:/postgres -datasource-context.xml"}) 

and create one more datasource context file for it. Unfortunately this way doesn't look like a good solution.

Is there a better way to solve my problem?

3 Answers 3

1

I think that the simplest solution is to keep a test class as the base one:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/mysql-datasource-context.xml"})
public class EmployeeDAOTest {

    @Autowired
    EmployeeDao employeeDao;

    @Test
    public void findAllTest() {
        assertTrue(employeeDao.findByName("noname").size() == 0);
    }
}

and then creating one empty subclass for postgres with its own configuration:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/postgres-datasource-context.xml"}, inheritLocations=false)
public class EmployeeDAOTestPostgres extends EmployeeDAOTest  {
}

As other suggested you can alter your Spring config files in order to have only one; you can for example put the datasource in a separate context and import it or use a profile (see here for an example)

2
  • Unfortunately that employeeDAO in the parent class isn't autowired without the ContextConfiguration annotation on the class. Jun 11, 2015 at 14:37
  • @user2246018: you are right you can use the mysql one as the base class and use an empty one for postgres; I updated the response
    – Giovanni
    Jun 11, 2015 at 14:45
0

At a glance is the spring multiple data source configure, actually you can get a lot posts for this via google or quickly search it in stackoverflow

Example: Spring Boot Multiple Datasource

Another solution I can image is using the spring profile.

0

I always find it's best to have a top level app context file which includes other files:

appcontext-root.xml

<beans>
    <import resource="appcontext-services.xml"/>
    <import resource="appcontext-db.xml"/>
</beans>

Then your application can run the context-root.xml, but your tests can test one (or more) of the lower level files.

If you want a swappable back end, you might consider using a PropertyPlaceHolderConfigurer.

eg:

<import resource="appcontext-db-${vendor}.xml"/>

And have a appcontext-db-mysql.xml, appcontext-db-postgres.xml along with System.setProperty("vendor", "mysql")

2
  • How does this help me to make it run the same test with two different datasources after each other during the package phase for example? Jun 11, 2015 at 14:38
  • @ContextConfiguration is simply a convenience. If you want multiple contexts for a single test, you might need to explicitly use ClassPathXmlApplicationContext instead.
    – lance-java
    Jun 11, 2015 at 15:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.