15 Nov 21:08
How to Spy a Method
Hey all, need a little help here.
I've been writing unit tests now for about a month or so, retroactively.
That is, we are trying to get to a test-driven development paradigm, but for
now, we need to cover our existing code base. We are using JUnit 3 and
JMock 2.5.1.
The issue is as such:
I have a method of a class that I wish to test. It does certain things
depending on parameter state, like any good ole method. However, at the end
of this method's execution, it daisy-chains to another method. This second
method is not one that we wish to test, especially as its code just creates
an HTTP connection to a URL and posts a message there. I want to write my
tests and expectations in such a way that I can expect the method
invocation, but not actually execute the code in that method.
Now, having done quite a bit of reading and Googling about this, I have not
been able to find the answer I need, but what I want to do is apparently
called 'method spying'. Any idea how to do this? This is a scenario where
I must stay within the constraints of the existing environment (no new jars,
no new frameworks - just JUnit and JMock).
Example:
public class SomeClass
{
private static final SomeClass INSTANCE = new SomeClass();
(Continue reading)
) you're ever allowed to
introduce new jars into your project.
Regards,
Andy
-----Original Message-----
From: DispatchMediaGroup [mailto:
.
There are some benefits to that structure.
The factory is a static singleton, but by having a class that is only concerned with the creation of an
object, you are aligning your code with the Single Responsibility Principle.
The SomeClass object is now testable and can be created by an IOC container if you get a chance to move at a
later date.
By extracting the HTTP messaging call into another class, you have also removed the messaging code from
"SomeClass", creating a better separation of concerns.
This allows you to reuse your http code elsewhere, and easily replace it if you change your messaging medium.
It doesn't rely on exposing internal methods for testing purposes.
What kind of an app are you refactoring? A Web-app, library, thick client, command line etc?
Kind Regards,
Andy
-----Original Message-----
From: DispatchMediaGroup [mailto:
RSS Feed