JUnit3:
TestActionJUnit3Test
import static org.apache.commons.lang.StringUtils.replace;
import lombok.SneakyThrows;
import org.apache.struts2.StrutsSpringTestCase;
import com.opensymphony.xwork2.ActionProxy;
public class TestActionJUnit3Test extends StrutsSpringTestCase {
@SneakyThrows
public void test() {
ActionProxy proxy = getActionProxy("/test/test.action");
String result = proxy.execute();
assertEquals("success", result);
assertEquals("Test by Linus", ((TestAction) proxy.getAction()).getMessage());
executeAction("/test/test");
assertFalse(((TestAction) findValueAfterExecute("action")).hasFieldErrors());
assertEquals("Test by Linus", findValueAfterExecute("message"));
}
@Override
protected String[] getContextLocations() {
return new String[] { "classpath:" + replace(getClass().getName(), ".", "/") + "-context.xml" };
}
}
上面我將預設讀取的Spring XML路徑改成和@ContextConfiguration一致。import lombok.SneakyThrows;
import org.apache.struts2.StrutsSpringTestCase;
import com.opensymphony.xwork2.ActionProxy;
public class TestActionJUnit3Test extends StrutsSpringTestCase {
@SneakyThrows
public void test() {
ActionProxy proxy = getActionProxy("/test/test.action");
String result = proxy.execute();
assertEquals("success", result);
assertEquals("Test by Linus", ((TestAction) proxy.getAction()).getMessage());
executeAction("/test/test");
assertFalse(((TestAction) findValueAfterExecute("action")).hasFieldErrors());
assertEquals("Test by Linus", findValueAfterExecute("message"));
}
@Override
protected String[] getContextLocations() {
return new String[] { "classpath:" + replace(getClass().getName(), ".", "/") + "-context.xml" };
}
}
JUnit4:
TestActionJUnit4Test
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.apache.struts2.StrutsSpringJUnit4TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.opensymphony.xwork2.ActionProxy;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestActionJUnit4Test extends StrutsSpringJUnit4TestCase<TestAction> {
@Override
protected String getConfigPath() {
return "struts-plugin.xml";
}
@Test
public void testExecute() throws Exception {
ActionProxy proxy = getActionProxy("/test/test.action");
String result = proxy.execute();
assertEquals("success", result);
assertEquals("Test by Linus", ((TestAction) proxy.getAction()).getMessage());
executeAction("/test/test");
assertFalse(getAction().hasFieldErrors());
assertEquals("Test by Linus", findValueAfterExecute("message"));
}
}
上面getConfigPath()可以回傳其它Struts2的XML檔,利用這個method可以啟動其它plugins,但JUnit3的寫法不需要這個動作。
import static org.junit.Assert.assertFalse;
import org.apache.struts2.StrutsSpringJUnit4TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.opensymphony.xwork2.ActionProxy;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestActionJUnit4Test extends StrutsSpringJUnit4TestCase<TestAction> {
@Override
protected String getConfigPath() {
return "struts-plugin.xml";
}
@Test
public void testExecute() throws Exception {
ActionProxy proxy = getActionProxy("/test/test.action");
String result = proxy.execute();
assertEquals("success", result);
assertEquals("Test by Linus", ((TestAction) proxy.getAction()).getMessage());
executeAction("/test/test");
assertFalse(getAction().hasFieldErrors());
assertEquals("Test by Linus", findValueAfterExecute("message"));
}
}