Stripes 框架单元测试:MockServletContext 给出 NullPointerException

发布于 2024-10-16 12:42:41 字数 2620 浏览 2 评论 0原文

编辑:您可以忽略我在下面写的大部分内容:

当我在某些 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.(MockRoundtrip.java:96) 在 net.sourceforge.stripes.mock.MockRoundtrip.(MockRoundtrip.java:82) 在 sempedia.tests.action.SeedSearchActionBeanTest.seedSearchTest(SeedSearchActionBeanTest.java:17) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

我猜这行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-10-23 12:42:41

错误在于这一行:

 filterParams.put("ActionResolver.Packages", "net.sourceforge.stripes");` 

这应该是(在我的例子中):

filterParams.put("ActionResolver.Packages", "action");

本质上,您正在设置找到 ActionBeans 的包名称。一旦你知道了,它看起来就很明显了。

The mistake is with this line:

 filterParams.put("ActionResolver.Packages", "net.sourceforge.stripes");` 

This should be (in my case):

filterParams.put("ActionResolver.Packages", "action");

Essentially you are setting the package name where the ActionBeans are found. It seems very obvious once you know it.

你丑哭了我 2024-10-23 12:42:41

您似乎正在测试是否已设置 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文