开发者

Stripes framework unit testing: MockServletContext giving NullPointerException

开发者 https://www.devze.com 2023-02-09 15:00 出处:网络
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:

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.source开发者_开发技巧forge.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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消