Hi Francois,
Thanks for your explanation.
I have another question thought. How should i write the equivalent of
methods 'dynamic.Mock.expect(String methodName)' and
'dynamic.Mock.expectAndReturn(String methodName, java.lang.Object obj)' in
own Mock object?
Is there any sample available?
Thanks lots.
Veny
-----Original Message-----
From: Francois Beausoleil [mailto:fb...@us...]
Sent: Tuesday, September 16, 2003 11:43 AM
To: ve...@p2...
Subject: Re: [MO-java-dev] creating a dynamic mock error -
com.packagename.classname is not an interface.
As you have noticed, you cannot mock classes. For your information,
java.util.List is an interface, not a class. ArrayList and LinkedList
are actual implementations of the List interface.
To mock concrete classes, you have to resort to the old way:
public class ObjectUnderTest {
public void method(Object arg) {
// Do something
}
}
public class MockObjectUnderTest extends ObjectUnderTest implements
Verifiable {
private ExpectationValue expectedArg = new ExpectationValue("arg");
public void setExpectedArg(Object arg) {
expectedArg.setExpected(arg);
}
public void method(Object arg) {
expectedArg.setActual(arg);
// Might call into super class to execute the code for real, or
not,
// depending on your needs.
}
public void verify() {
// This asserts that all Verifiable objects in this class have
their
// verify() methods called, and any exceptions thrown up the
// call stack
Verifier.verifyObject(this);
}
}
That is why most programmers and designers recommend coding against
interfaces. This helps because you can then send in a mock
implementation. DynaMocks are simpler to setup than old-style Mocks.
I sure hope that helps !
If you have other questions, do not hesitate !
François
On Tue, 16 Sep 2003 10:06:14 +0800, "veny" <ve...@p2...> said:
> Hi all,
>
> I've been using DynaMock to create mock class from interfaces. It works
> great. However, when i tried to mock my classes, there's always this
> error:
> "com.packagename.classname is not an interface". I've tried to mock java
> class (e.g. java.util.List), it works. But why creating of mock of my
> custom
> class doesn't? Any idea?
>
> Thanks.
>
> Veny
Developer of Java Gui Builder
http://jgb.sourceforge.net/
|