| Author |
Junit testing for Struts2
|
jignesh tandel
Greenhorn
Joined: Feb 16, 2012
Posts: 4
|
|
Junit testing for Struts2
Hi i am using Junit 4 to test struts2 action class.
I have BaseStrutsTest.java . my TestLoginAction.java extends BasdeStrutsTest.java
------------------------------------
----------------------
TestLoginAction.java
-----------
I tun "TestLoginAction.java" in Eclipse.. using "Run As " -- > "Junit .." it throws the "NullPoniterException" as dispatcher.init() method in "BaseStrutsTest.java"
Can you please hlep me for it ASAP?
Rgd,
Jignesh
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
Can you post the actual stack trace? It can't be that dispatcher is null since it is initialized one line above.
I do see one problem. The @Test shouldn't be there. Since you are extending TestCase, this is a JUnit 3.8 test. And @Test is a JUnit 4 concept.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
jignesh tandel
Greenhorn
Joined: Feb 16, 2012
Posts: 4
|
|
HI, Thanks for reply..
here is the actual stack trace.
java.lang.NullPointerException
at com.opensymphony.xwork2.util.ClassLoaderUtil.getResource(ClassLoaderUtil.java:86)
at com.opensymphony.xwork2.util.location.LocationUtils.getLocation(LocationUtils.java:288)
at com.opensymphony.xwork2.util.location.LocationUtils.getLocation(LocationUtils.java:202)
at com.opensymphony.xwork2.XWorkException.<init>(XWorkException.java:93)
at com.opensymphony.xwork2.XWorkException.<init>(XWorkException.java:76)
at com.opensymphony.xwork2.config.ConfigurationException.<init>(ConfigurationException.java:64)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadDocuments(XmlConfigurationProvider.java:135)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.init(XmlConfigurationProvider.java:100)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:130)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
at com.junit.testing.BaseStrutsTestCase.setUp(BaseStrutsTestCase.java:84)
at junit.framework.TestCase.runBare(TestCase.java:132)
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.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
--------------------------------------
Alternate, I used "StrutsTestCase". I extends it in"TestLoginAction"
there i show compilation error at
ActionProxy proxy = getActionProxy("/Login.action");
Please post your valuable commet in both the case
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
It's not obvious from the stack trace. The next step from the stack trace is to see what is wrong. This tells us that the problem is that the test can't find some file. I'd double check the classpath.
at com.opensymphony.xwork2.util.ClassLoaderUtil.getResource(ClassLoaderUtil.java:86)
If that doesn't work, you may need to look at the source code (open source is helpful) to see what specifically it is trying to load.
|
 |
jignesh tandel
Greenhorn
Joined: Feb 16, 2012
Posts: 4
|
|
Hi Jeanne
I tried with mention code. but there is still nullPointerexception during initalize the dispatcher. then i follow the other approach mention in below link.
http://www.javaranch.com/journal/2003/12/UnitTestingDatabaseCode.html
I successed with initail testing with loginDAO. now i would like to test addleaverecord() from DAO. so, do i need to mock the request ? or hard code the values in bean and passed that bean to addleaveracor() method,
if need to mock the request then how ? if i need to hard code the values then it is burden on developers to right code with testcase also?
which approach is feasible?
Can you please provide any example to test struts 2 with Junit ?
i asked more question :-)
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
In the case of that article, it doesn't matter whether you are using Struts2 or something else because you aren't testing the framework; just your logic. I like this approach. I test the framework through a browser. Either approach is feasible though.
When you are mocking you do hard code values. However the person writing this low level unit test is doing so at the same time as the code. It is on his/her mind.
I skipped the questions about how you'd test your code. As near as I can tell the code you are trying to test isn't in the thread. And the thread is long. When posting for help, it is best to have short self contained examples so someone can easily read them and see what you are trying.
|
 |
 |
|
|
subject: Junit testing for Struts2
|
|
|