Hi,
I'm new to
testing and having a hard time getting servletunit for
struts to work .
I've tried a few things but it is not happening.
I keep getting a null pointer exception.
The error message in
Junit seems to indicate that it is not seeing struts.config.
The place I have had struts.config located is the WEB-INF/lib directory .
I have tried copying the struts.config file every place imaginable in my Eclipse project including the project root (below).
Stepping through the debugger ,it is failing at the actionperform line.
The test class and error message are below.
I've been trying to get a test working for a couple days and would appreciate any help/insights.
Thanks.
public class NewUserMockStrutsTest extends MockStrutsTestCase {
String first_name = "";
String last_name = "";
String user = "";
String password ="";
String mainPhone = "";
String email = "";
String line1 = "";
String line2 = "";
String city = "";
String region ="";
String country = "";
String postcode = "";
String group_id = "";
String title = "";
public void setUp() throws Exception { super.setUp();
setConfigFile("C:\\_eclipseworkspace\\DocDist\\struts-config.xml");
first_name = "bob";
last_name = "hoskins";
user = "bobby";
password ="hosky";
mainPhone = "4i5-444-0090";
email = "bob@bob.com";
line1 = "line1 ddd";
line2 = "linew 2";
city = "sf";
region ="CA";
country = "US";
postcode = "4325-opoi";
group_id = "0";
title = "mr.";
}
public void tearDown() throws Exception { super.tearDown(); }
public NewUserMockStrutsTest(String testName) { super(testName); }
public void testActions() {
setContextDirectory(new File("C:\\_eclipseworkspace\\DocDist"));
// Test the list action
/* setRequestPathInfo("/newUser");
actionPerform();
verifyForward("failure");
verifyNoActionErrors();
*/
// Test the detail action - without an index
setRequestPathInfo("/newUser");
addRequestParameter( "first_name" ,first_name);
addRequestParameter( "last_name",last_name );
addRequestParameter( "user",user );
addRequestParameter( "password",password );
addRequestParameter( "mainPhone", mainPhone );
addRequestParameter( "email" ,email );
addRequestParameter( "line1" ,line1 );
addRequestParameter( "line2",line2 );
addRequestParameter( "city",city );
addRequestParameter( "region",region );
addRequestParameter( "country",country );
addRequestParameter( "postcode",postcode );
addRequestParameter( "group_id",group_id );
addRequestParameter( "title",title );
actionPerform();
verifyForward("failure");
verifyNoActionErrors();
getRequest().setAttribute(Globals.ERROR_KEY,null);
/*
// Test the detail action - with an index
clearRequestParameters();
setRequestPathInfo("/detail");
addRequestParameter("index","1");
actionPerform();
verifyForward("detail");
verifyNoActionErrors();
*/
}
public static void main(String[] args) {
junit.textui.TestRunner.run(NewUserMockStrutsTest.class);
}
}
May 13, 2009 6:42:55 PM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.util.LocalStrings', returnNull=true
May 13, 2009 6:42:55 PM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
May 13, 2009 6:42:55 PM org.apache.struts.action.ActionServlet initServlet
SEVERE: null
java.net.MalformedURLException
at java.net.URL.<init>(URL.java:601)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:947)
at org.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityManager.java:893)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:846)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:264)
at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:513)
at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:595)
at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1125)
at org.apache.commons.digester.Digester.parse(Digester.java:1567)
at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1433)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:466)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:326)
at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:394)
at com.paul.struts.action.NewUserMockStrutsTest.testActions(NewUserMockStrutsTest.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
May 13, 2009 6:42:55 PM servletunit.struts.MockStrutsTestCase getActionServlet
SEVERE: Error initializing action
servlet
javax.servlet.ServletException
at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1437)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:466)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:326)
at servletunit.struts.MockStrutsTestCase.actionPerform(MockStrutsTestCase.java:394)
at com.paul.struts.action.NewUserMockStrutsTest.testActions(NewUserMockStrutsTest.java:73)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
May 13, 2009 6:42:55 PM org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.action.ActionResources', returnNull=true
May 13, 2009 6:42:55 PM org.apache.struts.action.ActionServlet initServlet
SEVERE: null
java.net.MalformedURLException
at java.net.URL.<init>(URL.java:601)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:947)
at org.apache.xerces.impl.XMLEntityManager.startEntity(XMLEntityManager.java:893)
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(XMLEntityManager.java:846)
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(XMLDocumentScannerImpl.java:264)
at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:513)
at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:595)
at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)
at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1125)
at org.apache.commons.digester.Digester.parse(Digester.java:1567)
at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1433)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:466)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:326)
at servletunit.struts.MockStrutsTestCase.tearDown(MockStrutsTestCase.java:130)
at com.paul.struts.action.NewUserMockStrutsTest.tearDown(NewUserMockStrutsTest.java:44)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
May 13, 2009 6:42:55 PM servletunit.struts.MockStrutsTestCase getActionServlet
SEVERE: Error initializing action servlet
javax.servlet.ServletException
at org.apache.struts.action.ActionServlet.initServlet(ActionServlet.java:1437)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:466)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:326)
at servletunit.struts.MockStrutsTestCase.tearDown(MockStrutsTestCase.java:130)
at com.paul.struts.action.NewUserMockStrutsTest.tearDown(NewUserMockStrutsTest.java:44)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)