编辑:您可以忽略我在下面写的大部分内容:
当我在一些 TestNG 代码中执行以下操作时,我得到了一个空值的上下文:
public void setupNonTrivialObjects() {
TestFixture.context = new MockServletContext("test");
}
我应该做更多的事情来制作一个不为空的 MockServletContext 对象吗?
原文:我正在学习将 Stripes 框架与 TestNG 一起使用。
我在这里遵循示例(但将其调整为我自己的代码): http: //www.stripesframework.org/display/stripes/Unit+Testing在标题方法 2下
我有这个测试:
public class SeedSearchActionBeanTest {
@Test
public void seedSearchTest() throws Exception {
// Setup the servlet engine
MockServletContext ctx = TestFixture.getServletContext();
MockRoundtrip trip = new MockRoundtrip(ctx, SeedSearchActionBean.class);
trip.setParameter("input", "sdfs");
trip.execute();
SeedSearchActionBean bean = trip.getActionBean(SeedSearchActionBean.class);
Assert.assertEquals(bean.getInput(),"sdfs");
Assert.assertEquals(trip.getDestination(), "/results.jsp");
}
}
这个“TestFixture”不太确定那是什么。
public class TestFixture {
private static MockServletContext context;
@BeforeSuite
public void setupNonTrivialObjects() {
TestFixture.context = new MockServletContext("test");
// Add the Stripes Filter
Map<String,String> filterParams = new HashMap<String,String>();
filterParams.put("ActionResolver.Packages", "net.sourceforge.stripes");
context.addFilter(StripesFilter.class, "StripesFilter", filterParams);
// Add the Stripes Dispatcher
context.setServlet(DispatcherServlet.class, "StripesDispatcher", null);
}
public static MockServletContext getServletContext() {
return TestFixture.context;
}
}
我收到这个错误
FAILED: seedSearchTest
java.lang.NullPointerException
at net.sourceforge.stripes.mock.MockRoundtrip.getUrlBindingStub(MockRoundtrip.java:384)
at net.sourceforge.stripes.mock.MockRoundtrip.<init>(MockRoundtrip.java:96)
at net.sourceforge.stripes.mock.MockRoundtrip.<init>(MockRoundtrip.java:82)
at sempedia.tests.action.SeedSearchActionBeanTest.seedSearchTest(SeedSearchActionBeanTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
我想这条线MockServletContext ctx = TestFixture.getServletContext();
不起作用,我想知道我是否缺少某些东西,特别是在 web.xml 中有什么我必须做的吗?