aspose file tools*
The moose likes Struts and the fly likes JUnit and mocking objects in Struts Action class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JUnit and mocking objects in Struts Action class" Watch "JUnit and mocking objects in Struts Action class" New topic
Author

JUnit and mocking objects in Struts Action class

Dan Nuttle
Greenhorn

Joined: Sep 24, 2003
Posts: 2
Has anyone run into this problem? Let's say you have something like this in your Action class:
MyObject o = new MyObject();
But you want to mock that object when you're creating a JUnit test for your action class. Now, with an ordinary class, you'd just create the mock object in your Test class, and send it to your Action class through a setter. But according to sourceforge, you should not create your Action class directly when testing it. You let Struts create it. So your Test class has no reference to your Action class. In fact, the Action object hasn't even been created when the Test class calls actionPerform().
How do I mock up this object, configure it the way I want, and then save it so the Action class can get it? The best I can come up with so far is to subclass my Action class, and when testing, have Struts call the subclass. I override the getter for MyObject in the subclass, and have it retrieve the mocked object from the HttpSession. (That's where I store the mocked object in my Test class.)
My architect thinks this is a phony and bad approach. But I can't come up with anything else!
Anybody?
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

I kind of feel that this question belongs in the Testing forum but I'll ask you to elaborate on a few things before deciding to move it there:
1. Why are you creating an object in your Action? Does the object truly belong in the Action? Would it be more appropriate in a business tier object that the action delegates to? Would it be easier to test if you refactored to that?
2. Are you using JUnit or some extension to JUnit? If you're using JUnit, I don't see any straightforward way to let the Struts framework create your action.


Junilu - [How to Ask Questions] [How to Answer Questions]
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
Originally posted by Junilu Lacar:
2. Are you using JUnit or some extension to JUnit? If you're using JUnit, I don't see any straightforward way to let the Struts framework create your action.

Take a look at StrutsTestCase. It is pretty sweet for Unit Testing Struts Applications.
As far as the original question... why not introduce some type of Factory Method for the creation of the object. During testing the Factory could be configured to hand back your mock object. However, I have to agree with Junilu, we don't really know enough about what you are doing to really give a good suggestion.
BTW, welcome to JavaRanch!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit and mocking objects in Struts Action class
 
Similar Threads
System Tests
unit testing struts?
Overide a method at runtime
Spring 3 in Action
Non J2EE transactional management in Java