Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

JUnit and mocking objects in Struts Action class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 14028
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
rubbery bacon. rubbery tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!