简体   繁体   中英

Struts testing JUnit4

A project that Im working on uses Spring and Struts.

Up until now, we we using using Struts 2.1.8.1. All my unit tests extended the StrutsSpringTestCase which in turn extended the StrutsTestCase . However, this used JUnit 3.8.2 and hence had no annotation support.

For Struts 2.2.3, the dev team added a Junit4 TestCase , called StrutsJUnit4TestCase . This test case however, does not the have the supporting functions that StrutsTestCase did eg for issuing struts actions. I tried to find some tutorials using this new test case, but haven't had any luck.

I should mention that I'm trying to use the Rollback annotations, to rollback the automatically rollback database changes that occur during the test.

How exactly am I supposed to use this new StrutsJunit4TestCase ?

Struts2 actions are nothing but simple classes. I suggest you to test them the way you test other classes IMHO.
I've been doing this way for most of my time doing struts2 and barely felt the need to use StrutsTestCase . If you design your actions with DI in mind you can inject any dependencies while testing the action.

at least the following one works in my computer

public class BlaBlaBlaTestCase extends StrutsSpringTestCase {
    private final Logger log = Logger.getLogger(this.getClass());

    @Override
    public String getContextLocations() {
        return "applicationContext_test.xml";
    }

    @Test
    public void testWhatever() {
                //...
    }
}

the getContextLocations method is important, because this is the method which defines where your applicationContext for testing is.

oh, i'm using junit 4.4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM