<img name="Z1AG7C5BA4E53BEF3B5B4C51986D582D6FF57883" src="http://files.note.sdo.com/Z1AG7C5BA4E53BEF3B5B4C51986D582D6FF57883" _src="http://files.note.sdo.com/Z1AG7C5BA4E53BEF3B5B4C51986D582D6FF57883"><br><br>import org.jmock.Mockery;
import org.jmock.Expectations;
class PublisherTest extends TestCase {
Mockery context = new Mockery();
public void testOneSubscriberReceivesAMessage() {
// set up
final Subscriber subscriber = context.mock(Subscriber.class);
Publisher publisher = new Publisher();
publisher.add(subscriber);
final String message = "message";
// expectations
context.checking(new Expectations() {{
one (subscriber).receive(message);
}});
// execute
publisher.publish(message);
// verify
Assert.assert...//context.assertIsSatisfied();
}
}
注意:对于被测试方法里包含(方法又包含(需要mock的对象))这种情况,mock依然支持。使用mock做测试,一定要保证测试方法中mock对象的参数和被测试对象中的参数保持一致,否则mock对象就不能返回will()结果。