stubbing methods on test objexct with jMock

P

PeterS

Hi together,

I am using jMock for doing my unit tests and it works fine. But there
is one point I do not know how to solve it. I want to test a method (
e.g. "testMethod")in my object which calls another method (e.g.
"anotherMethod") of the same object. I do not want to execute
anotherMethod it´s own code. Instead it shall return a value
controlled by the test case class. Can someone tell me what I have to
do to achieve this aim with jMock?

Many thanks

With best regards

Peter
 
B

Bryce

I am using jMock for doing my unit tests and it works fine. But there
is one point I do not know how to solve it. I want to test a method (
e.g. "testMethod")in my object which calls another method (e.g.
"anotherMethod") of the same object. I do not want to execute
anotherMethod it´s own code. Instead it shall return a value
controlled by the test case class. Can someone tell me what I have to
do to achieve this aim with jMock?

I'm not sure what you are trying to do.

I think you are saying that you have a class, like this:

public class MyClass {
public void testMethod() {
String temp = anotherMethod();
}

public String anotherMethod() {
....
}
}

Is this the case?

Then, assuming you are testing MyClass, its not possible to do it as
written. However, if you've written your class in another way you
could (I'm throwing this together quickly, so it may not compile, but
gives you an outline):

public class MyClass {
private AnotherClass anotherClass;

public void setAnotherClass(AnotherClass ac) {
this.anotherClass = ac;
}

public void testMethod() {
String temp = anotherClass.anotherMethod();
}
}

public interface AnotherClass {
public String anotherMethod();
}

public class AnotherClassImpl implements AnotherClass {
public String anotherMethod() {
...
}
}

Then, your test code would look like this:

public class TestMyClass extends MockObjectsTestCase{
public void testTestMethod() {
MyClass myClass = new MyClass();

Mock mock = new Mock(AnotherClass.class);
myClass.setAnotherClass((AnotherClass)myClass.proxy);


mock.expects(once).method("anotherMethod").withNoArguments().will(returnValue("testString));

myClass.testMethod();

mock.verify();
}
}

I suppose an easier way to do it, without using Mocks is to just
subclass your class, and override the method you need to.:

public class MyClassMock extends MyClass {
public String anotherMethod() {
return "Hard coded string";
}
}
 
P

PeterS

Bryce said:
I'm not sure what you are trying to do.

I think you are saying that you have a class, like this:

public class MyClass {
public void testMethod() {
String temp = anotherMethod();
}

public String anotherMethod() {
....
}
}

Is this the case?

Then, assuming you are testing MyClass, its not possible to do it as
written. However, if you've written your class in another way you
could (I'm throwing this together quickly, so it may not compile, but
gives you an outline):

public class MyClass {
private AnotherClass anotherClass;

public void setAnotherClass(AnotherClass ac) {
this.anotherClass = ac;
}

public void testMethod() {
String temp = anotherClass.anotherMethod();
}
}

public interface AnotherClass {
public String anotherMethod();
}

public class AnotherClassImpl implements AnotherClass {
public String anotherMethod() {
...
}
}

Then, your test code would look like this:

public class TestMyClass extends MockObjectsTestCase{
public void testTestMethod() {
MyClass myClass = new MyClass();

Mock mock = new Mock(AnotherClass.class);
myClass.setAnotherClass((AnotherClass)myClass.proxy);


mock.expects(once).method("anotherMethod").withNoArguments().will(returnValue("testString));

myClass.testMethod();

mock.verify();
}
}

I suppose an easier way to do it, without using Mocks is to just
subclass your class, and override the method you need to.:

public class MyClassMock extends MyClass {
public String anotherMethod() {
return "Hard coded string";
}
}


Hi Bryce,

your understanding is correct. That´s exactly what I wanted to do.
Subclassing was also an idea I had but I hoped that there would be
another possibilty without writing a new class. So I have to use one
of the two methods - delegating or subclassing. Many thanks for your
answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top