我有一个使用Spring和Struts2的应用程序,我想使用Junit将TDD Test添加到actions类中。所以使用Struts Junit plugin Page上的指南,我构建了以下类:
行动类:
package articulos.aumentoprecio.controlador;
import com.opensymphony.xwork2.ActionSupport;
import articulos.aumentoprecio.transporte.AumentoPrecioPojo;
public class MostrarAumentoPrecioAction extends ActionSupport {
private AumentoPrecioPojo aumentoPrecio = new AumentoPrecioPojo();
@Override
public String execute() throws Exception {
return SUCCESS;
}
public AumentoPrecioPojo getAumentoPrecio() {
return aumentoPrecio;
}
public void setAumentoPrecio(AumentoPrecioPojo aumentoPrecio) {
this.aumentoPrecio = aumentoPrecio;
}
}
和Junit测试用例:
package articulos.aumentoprecio.controlador;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.opensymphony.xwork2.ActionProxy;
import org.apache.struts2.StrutsJUnit4TestCase;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext.xml"})
public class MostrarAumentoPrecioActionTest extends StrutsJUnit4TestCase {
public MostrarAumentoPrecioActionTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testExecute() throws Exception {
ActionProxy proxy = getActionProxy("/MostrarMenu.action");
MostrarAumentoPrecioAction controlador = (MostrarAumentoPrecioAction) proxy.getAction();
String result = proxy.execute();
assertEquals("El submodulo AumentoPrecio no esta integrado", "success", result);
}
}
然而,当执行TDD时,在方法getActionProxy中显示NullPointerException。
输出窗口中的stacktrace show是:
Testcase: testExecute(articulos.aumentoprecio.controlador.MostrarAumentoPrecioActionTest): Caused an ERROR
null
java.lang.NullPointerException
at org.apache.struts2.StrutsJUnit4TestCase.getActionProxy(StrutsJUnit4TestCase.java:142)
Test articulos.aumentoprecio.controlador.MostrarAumentoPrecioActionTest FAILED
struts指南中是否有任何额外的配置?