January 16, 2006 22:22
Java, Blog
So far I've been focusing on Webwork. However, I've also been trying more automated testing again. My previous effort didn't work so well, as I spent more time writing the mockobjects library than writing the unit tests, but I was happy to give it another try using TestNG.

Unit tests work fine for me, but I'm still not fascinated by them.

Things got more interesting when I discovered the integration testing classes in Spring. I found that the Spring integration test classes were built on top of JUnit, and didn't seem to support TestNG. So I switched, and it was so worth it.

Integration testing with Spring is awesome. I can write some code, write an integration test, then run through the test until I'm sure I know what's going on. This really helps when parsing posts. A raw post goes through several different parsers to do things like colorize Java code, bleep swearwords and escape HTML characters, so there are many opportunities to parse things in the wrong order or not at all.

Here's a sample:

public class DisplayPostActionTest extends
        AbstractDependencyInjectionSpringContextTests {
    @Override
    protected String[] getConfigLocations() {
        return fakeDatabasePaths;
    }

    private static String[] fakeDatabasePaths = {
        "classpath*:/applicationContext-test.xml", "classpath*:/applicationContext.xml", 
    };
    
    DisplayPostAction displayPostAction;
    
    /* (non-Javadoc)
     * @see org.springframework.test.AbstractDependencyInjectionSpringContextTests#onSetUp()
     */
    @Override
    protected void onSetUp() throws Exception {
        ApplicationContext context = applicationContext;        
        displayPostAction = (DisplayPostAction) context.getBean("displayPostAction");
    }
    
    public void testExecute() {        
        try {
            displayPostAction.setPostId("43");
            String returnValue = displayPostAction.execute();
            assertNotNull(returnValue);
            
            assertTrue(Action.SUCCESS.equals(returnValue));
            
            Post post = displayPostAction.getPost();
            assertNotNull(post);            
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
}

This works great in the simple case where you’re using HibernateTransactionManager and the OpenSessionInView class in conjunction with HibernateTemplate. If you’re not... well, the next post is about that.

« Hibernate vs. Spring | Home | My Office »

galactodensimeter altarage peaberry indubitableness homoplasis gush toco gemminess Mystic http://www.wiresource.com/

name
url