Wednesday, August 12, 2009

Using Spring with multiple databases Part 5, Unit Testing using JUnit 4 and Annotations

One of the great features of the Spring framework is they have create support for Unit Testing in JUnit. Also you have the ability to use Annotations to cut down on the amount of xml you need to get your test code up and running.
Here is a example that I used to test my Login Controller:

package com.endurotracker.gwt.server.web.controllers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.runner.RunWith;
import org.junit.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.endurotracker.gwt.dao.UsersDao;
import com.endurotracker.gwt.model.ServerSideUser;
import com.endurotracker.gwt.model.Users;
import com.endurotracker.gwt.server.service.UserService;

* @author David
@ContextConfiguration(locations = { "/applicationContext.xml","/jndi.xml","/applicationContext-acegi-security.xml" })
@TransactionConfiguration(transactionManager="jtaTransactionManager", defaultRollback=false)
public class LoginControllerTest {

private DaoAuthenticationProvider daoAuthenticationProvider;
private UsersDao userDetailsService;
private UsersDao usersDao;
private EhCacheBasedUserCache userCache;

private UserService userService;
LoginController loginController;

private static final Logger log = Logger.getLogger(LoginControllerTest.class);

public void testLogin()
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("admin","pwd");

Authentication auth = daoAuthenticationProvider.authenticate(token);

//set SecurityContext
SecurityContext context = new SecurityContextImpl();

Object obj = auth.getPrincipal();
String username = "";

if (obj instanceof UserDetails) {
username = ((UserDetails) obj).getUsername();
} else {
username = obj.toString();

//place in Cache
log.debug("loadUserByUsername " + username);

ServerSideUser serverUser = null;

boolean useCache = true;

if (useCache) {
serverUser = (ServerSideUser) userCache

List users;
Users u;
if (serverUser == null) {

users = usersDao.GetByProperty("username", username);
if(users.size() > 0)
u = users.get(0);
userCache.putUserInCache(new ServerSideUser(u));
u = null;


//Test if user can now be found, after being placed in cache
Users user = userService.getCurrentUser();

loginController = new LoginController(user);



public void testShowLogin()
loginController = new LoginController();
String returnViewname = loginController.showHandler();


Hopefully, these posts will help some people who had very little success previously finding good examples on how to setup Spring with multiple databases (datasources).
I know I did.

1 comment:

  1. I think Spring can't help you here. Spring provides nice and clean transaction handling of Hibernate's Session, DAO and template exposure, but as far as object graph loading is concerned, it all depends on Hibernate API. I had similar request. I then wrote a small persistant library. Now I would look for different solution - maybe ' front-ending the databases with third party software'.