Webwarp Util Test

General

This module simplifies test setup when implementing tests that need a Spring context. Additionally automated dependency injection (auto-wiring) can be used. 

Class Overview

Examples

Implementing a Spring context test

The example shows how a simple test is implemented that loads a Spring configuration file from /springContext.xml.

import net.sf.webwarp.util.test.AbstractSpringContextTest; import org.junit.Assert; import org.junit.Test; public class SpringContextTest extends AbstractSpringContextTest { @Override protected String[] getConfigLocations() { return new String[]{"/springContext.xml" }; } @Test public void testSomething() { MyBean bean = (MyBean)getApplicationContext().getBean("myBean"); bean.testMe(); } }

Adding AutoWiring

The test above can be simplified by using the autowiring feature: simply inherit from the autowiring capable base class and provide a corresponding property setter method as shown:

import net.sf.webwarp.util.test.AbstractDependencyInjectionSpringContextTest; import org.junit.Assert; import org.junit.Test; public class AutowiredSpringContextTest extends AbstractDependencyInjectionSpringContextTest{ private MyBean bean; public void setMyBean(MyBean bean){ this.bean = bean; } @Override protected String[] getConfigLocations() { return new String[]{"/springContext.xml"}; } @Test public void testSomething() { // MyBean bean = (MyBean)getApplicationContext().getBean("MyBean"); bean.testMe(); } }  

Implementing a transactional test case

For implementing transactional test cases the AbstractTransactionalSpringContextTest can be used. Several methods can be overridden for adapting the default behaviour or doing additional things, see the following code snippet:

import net.sf.webwarp.util.test.AbstractTransactionalSpringContextTest;

import org.junit.Assert; import org.junit.Test; public TransactionTests extends AbstractTransactionalSpringContextTest{ public TransactionalTests(){ setTransactionManager(<Get the platform transactionmanager>); } @Test public void rollBackAfterTest(){ // Your test code comes here setComplete(false); } @Test public void commitAfterTest(){ // Your test code here setComplete(true); } // Methods that can be overridden // protected void onSetUpBeforeTransaction() throws Exception {} // protected void onSetUpInTransaction() throws Exception {} // protected void onTearDownInTransaction() throws Exception {} // protected void onTearDownAfterTransaction() throws Exception {} }

Webwarp Util