Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

New to Mock Objects

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code base I am currently working in is much like Struts in nature, but homegrown (I know, I know, why not use Struts?). Anyhow I would like to be able to isolate the classes that do the business logic, like Struts Actions, and test them into oblivion. I have looked at several different frameworks, but am on overload.
The mockobjects.com set of classes seems to be the best approach for this type of testing as it does not require the starting of a server as Cactus does, but I have a hard time finding any good documention on something that is a non-trivial test.
Example code from a servlet:
UserHome home = request.getParameter("homepage");
if (home != null) {
request.setAttribute("MY_DATA", home.getUserData());
}

The first thing I discovered was that MockObjects gripped if I did not do a setupAddParameter() for something that may or may not be set. Yes, the null check style is not great but I don't want to refactor without having a series of tests in place. We got around this by just setting each parameter to null. (Probably a bad idea)
The next more painful discovering is that the mockobjects want to do all sorts of verification along the way. To start out with, I just want to do some basic object checking where I take object returned by getUserData and do my own asserts on the values inside the object.
I am hung up in a lot of the details about when to use setupXXX methods and how build some tests that give me the kind of control I am looking for. Maybe mockobjects is not the best route and EasyMock would be a better choice. I just would like some input before starting over after the time investment I have already made.
Examples that do what I am trying to do are perfectly acceptable. RTFM is fine to as long as it is more then the Javadocs on the code(bit sparse).
Thanks in advance,
-Kent
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your problem, in short, is that the com.mockobjects.foo.bar.Something class you're using is too strict about which methods the class under test can call? If this is the case, then what's wrong with just calling the setupXXX() methods the mock class expects you to call?
Regarding the use of EasyMock, I would try it. I'm using EasyMock myself almost exclusively these days -- except when the object I need to mock is not an interface but a concrete class (then I either write my own mock class, use a library such as MockObjects.com, or turn to JMock for its concrete class mocking feature!).
 
Kent Henneuse
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Lasse,
Yes the classes are overly strict about how many times a method gets called. We stuff data into the request via the setAttribute call. I want to pull out those objects to do assertions on them. What would really help is sample servlet and mockobject combination that does that sort of thing. I find the examples a bit to trivial and everytime I look on the web the interfaces seem to be very different.
I will dig into EasyMock.
 
author & internet detective
Posts: 39392
763
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kent,
With either mock objects framework, there is a count of how many times a method gets called. If you don't call the verify method, the minimum number of method calls won't be enforced.
I've only been using easymock a couple days, but I think if you create a nice control, you will get the behavior you are looking for.
 
sharp shooter, and author
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrestled with this myself a little while ago and also looked at some of the mock frameworks. Because I wanted something quick and easy, I created my own mock objects. I may eventually switch to using one of the prebuilt collections of mocks/frameworks, but for the moment my implementations satisfy my needs well.
As an example, I have an action called ChangeCategoryAction and some test code in ChangeCategoryActionTest that makes use of a simple MockHttpServletRequest. It's not pretty or clever but it does the job - for the moment anyway!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!