Stripes 框架单元测试:MockServletContext 给出 NullPointerException
编辑:您可以忽略我在下面写的大部分内容:
当我在某些 TestNG 代码中执行以下操作时,我得到上下文的 null 值:
public void setupNonTrivialObjects() {
TestFixture.context = new MockServletContext("test");
}
我应该做更多的事情来制作不为 null 的 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 在net.sourceforge.stripes.mock.MockRoundtrip.getUrlBindingStub(MockRoundtrip.java:384) 在 net.sourceforge.stripes.mock.MockRoundtrip.
我猜这行 MockServletContext ctx = TestFixture.getServletContext();
不起作用,我想知道是否有什么我特别想念,我必须在 web.xml 中做些什么吗?
EDIT: You can ignore most of what I have written below:
I am getting a null value of context when I do the following in some TestNG code:
public void setupNonTrivialObjects() {
TestFixture.context = new MockServletContext("test");
}
Am I supposed to do something more to make a MockServletContext object that is not null?
ORIGINAL: I am learning to use the Stripes Framework with TestNG.
I am following the example here (but adapting it to my own code): http://www.stripesframework.org/display/stripes/Unit+Testing under the heading Approach 2
I have this test:
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");
}
}
This "TestFixture" not really sure what that is.
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;
}
}
I get this error
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)
I guess this line MockServletContext ctx = TestFixture.getServletContext();
is not working, I am wondering if there is something I am missing, particularly, is there something I have to do in the web.xml?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误在于这一行:
这应该是(在我的例子中):
本质上,您正在设置找到 ActionBeans 的包名称。一旦你知道了,它看起来就很明显了。
The mistake is with this line:
This should be (in my case):
Essentially you are setting the package name where the ActionBeans are found. It seems very obvious once you know it.
您似乎正在测试是否已设置 Stripes 操作 bean 创建和参数传递,毫无疑问,开发 Stripes 框架的人员已经对这些进行了广泛的测试。我倾向于测试从我的操作中调用的加载/保存/等业务逻辑(服务)。
You seem to be testing whether you've set up Stripes action bean creation and parameter passing which no doubt have been tested extensively by those developing the Stripes Framework. I tend to test the load/save/etc business logic (services) called from my actions.