0
votes

I am using struts1.2 and junit 3. In order to test my action classes, I need to create HttpServletRequest objects.

Now Struts 1.2 has does not provide a way to create mock objects of HTTPServletRequest. However, Struts1.3 has MockHttpServletRequest.class, with which you can mock the HTTPServletRequest.

It's not an option to upgrade from Struts 1.2 to Struts 1.3 at the moment. So what I did was create a jar file out of the mock package in Struts 1.3 and added it to my project.

Eclipse content tool does see the MockHttpServletRequest class, ie if I type "Mockhtt" and press CTRL+space, eclipse completes the class name for me and adds the import statement as well.

But after I create the HTTPServletRequest object, eclipse gives an error saying "The import org.apache.struts.mock cannot be resolved". enter image description here

What can I do to get around this error? I need to mock the HTTPServletRequest.

1
This is not a good idea. Instead, why don't you look into using EasyMock or Mockito? I personally use EasyMock in nearly every unit test I write to mock HttpServletRequests. - ninnemannk
Have you tried a clean project? When eclipse is upset, that sometimes helps. - flup
I am using Mockito.. but can you give me an example of how to mock a httpservletRequest? - Susie
This is what I initially did to create the mock and it didn't work so I resorted to creating my jar @Mock HttpServletRequest request; But how would you instantiate the request object? - Susie
classpath fix wizard would help by adding jars to your project - farmer1992

1 Answers

0
votes

Have you included the required jar file, perhaps struts-core-1.2.xx.jar in your project?

It seems like the jar file you have included in your project does not contain the referenced class.

Double check the jar file you have included and see if it actually contains the class.

You may wanna download the fresh copy of strut and copy the above jar file into your lib folder.

You can check if the class exist in the jar file by accessing the jar file via any unzipping sotware (winRAR) and see if the class exist as

"//org/apache/struts/mock/MockHttpServletRequest.class"

Hope it helps!