Author: EMU (Huang Xiwei)
JUnit does not test Struts Action, httpunit can only test the servlet. It is always my heart to make a unit test for the Action layer when using the struts. Now, we have strutstestcase. According to the online introduction, StrutstestCase should be very simple, as long as the JAR package is coming back to the project. It may actually be almost almost - if your luck is not as stinking.
Pick one of the mirror icons on the SourceForge: http: //areron.dl.sourceForge.Net/SourceForge/Strutstestcase/Strutstest213-1.2_2.4.zip
Flashget came back, put it in the JBuilder's UserHome, find a struts action to create test case, when you created, Test case's base class is changed to MockStrutstestcase, the method of testing is not selected (because we are specific to each logic for Action Branch test is not a specific method). Add a test after creating success:
Public void testusfulrefresh () {setRequestPathInfo ("/ refreshsystemData"); actionPerform (); VerifyForward ("Success");}
Hey, I carefully picked an experiment with no parameters.
Everything looks smooth. Run Test, the nightmare started:
java.lang.NullPointerException at servletunit.struts.MockStrutsTestCase.getActionServlet (MockStrutsTestCase.java:331) at servletunit.struts.MockStrutsTestCase.tearDown (MockStrutsTestCase.java:130) at hospital.tongren.oa.system.action.TestRefreshSystemDataAction.tearDown ( TestrefreshsystemDataAction.java:34)... (Click for Full Stack TRACE) ...
Fortunately, I didn't open the box, or it was a big gun.
It seems to be debugged, first go to the same place with Strutstestcase original code back http://aleron.dl.sourceforge.net/sourceforge/strutstestcase/strutste-213-src.zip
Adjust the Source inside UserHome. DEBUG in, keeping it with org.apache.struts.Action.ActionServlet, where the wrong place is:
InputStream INPUT = GetServletContext (). GetResourceAsStream ("/ web-inf / web.xml");
Try {Digester.Parse (Input);
} catch (ioexception e) {....
INPUT is an empty pointer. I don't know why servletContextSimulator is not able to find the WebModule location when simulating servletcontext. Search the Internet for a moment the document, found in this passage in http://strutstestcase.sourceforge.net/api/servletunit/struts/MockStrutsTestCase.html: NOTE: By default, the Struts ActionServlet will look for the file WEB-INF / Struts-config.xml, so you must place the directory That Contains Web-In Your Classpath. ...
Try it first, put the WebModule path into the classpath, useless. Looking down, I found this good stuff: setContextDirectory. Add a sentence in STARTUP:
THIS.SETCONTEXTDIRECTORY (New File ("E: // ProjectPath // WebModulePath //));
I finally gave the empty pointer. But report a new exception:
JUnit.framework.assertionFaileDError: Received error 400: Invalid Path / RefreshsystemData Was Requested
AT servletUnit.httpservletResponseSimulator.Senderror (httpservletresponsesimulator.java: 463)
At Org.Apache.Struts.Action.RequestProcessor.ProcessMapping (RequestProcessor.java: 684)
At Org.Apache.struts.Action.RequestProcessor.Process (RequestProcessor.java: 242)
At Org.apache.struts.Action.ActionServlet.Process (ActionServlet.java: 1482)
At Org.Apache.struts.Action.ActionServlet.dopost (ActionServlet.java: 525)
At servletUnit.struts.mockstrutstestcase.ActionPerform (Mockstrutstestcase.java: 394)
At Hospital.Tongren.TestReSystem.testrefreshsystemdataAction.testsuccessfulRefresh (TestRefreshsystemDataAction.java: 51)
... (Click for Full Stack TRACE) ...
Can't find the Path configured in Strutsconfig. Strutsconfig is configured in Web.xml, should still be web.xml, no found, then specify the position of the strutsconfig file: setConfigfile ("E: //....../struts-config.xml" );
It is finally running.
Subsequently, if setServletConfigfile ("e: //../Web-inf //web.xml"); MockStrutStestCase can also find the Strutsconfig file according to the configuration in Web.xml.
Finally, the absolute address E: // above is all changed to the relative address: setContextdirectory (New file ("ModulePath //"); setServletConfigfile ("ModulePath // Web-INF // Web.xml") ; // this.setconfigfile ("ModulePath // Web-INF // CONFIG ////// Struts-Config.xml"); blood spit, continue to depress, why don't others do not need to configure so trouble? I did something wrong in the end, or the fault of jbuilder?
CactusStrutstestcase did not match it, it seems to add a package, then look at the first.
Author: EMU (Huang Xiwei)