views:

34

answers:

0

How do I make sure that there is only 1 sessionfactory/session/transaction when i run a test case? Currently in my test case the DataBase changes (that i make in the test case) are not visible in the application code. Here is the base class of the test.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring.xml", "/test-context.xml" })
@TransactionConfiguration(transactionManager="transactionManager")
public abstract class BaseTestCase extends
AbstractTransactionalJUnit4SpringContextTests {

 @Resource
 protected SessionFactory sessionFactory;

 public Session getSession() {
  return sessionFactory.getCurrentSession();
 }
}

In my application there is a SpringUtil class that creates the applicationcontext and is widely used to get beans. In many places instead of dependency injection, this class is used to get dependencies using the getBean method. This seems to be the problem as it doesnt let the Junit framework to Dependency Inject the sessionfactory.

public class SpringUtil {
private static SpringUtil springUtil;
private final static String springXmlFile = "spring.xml";
private ApplicationContext applicationContext;

static {
 springUtil = new SpringUtil();
}

private SpringUtil() {
 init();
}

private void init() {
 applicationContext = new ClassPathXmlApplicationContext(springXmlFile);
}

 public static <T> T getBean(Class<T> clazz) {
  String beanName = clazz.getSimpleName();
  beanName = beanName.substring(0, 1).toLowerCase()
  + beanName.substring(1);
  return getBean(clazz, beanName);
 } 
}