今天在学习appFuse的工程中,学习了解了jMock

2006-04-27 16:59:06
Getting Started
开始
This guide assumes you are familiar with unit-testing and JUnit.
这个指南假定你熟悉单元测试和使用JUnit.

For a simple example we are going to test a publish/subscribe message system.
作为简单例子,我们将要测试一个[消息发布订阅系统]。

A Publisher sends objects to zero or more Subscribers.
一个发布者[Publisher]要发送对象到0个或者多个订阅者[Subscribers]。

We want to test the Publisher, which involves testing its interactions with its Subscribers.
我们想要测试发布者,包含了测试者和他订阅者的交互的测试。

The Subscriber interface looks like this:
订阅者接口的代码
 interface Subscriber {
  void receive(String message);
 }

We will test that a Publisher sends a message to a single registered Subscriber.

To test interactions between the Publisher and the Subscriber we will use a mock Subscriber object.

First we must import the jMock classes, define our test fixture class and define a test case method.

import org.jmock.*;

 class PublisherTest extends MockObjectTestCase {
  public void testOneSubscriberReceivesAMessage() {
  }
 }

We will now write the body of the testOneSubscriberReceivesAMessage method.

We first set up the context[上下文] in which our test will execute. We create a Publisher to test. We create a mock Subscriber that should receive the message. We then register the Subscriber with the Publisher. Finally we create a message object to publish.

 Mock mockSubscriber = mock(Subscriber.class); //设定上下文
 Publisher publisher = new Publisher();   //要测试的publisher
 publisher.add( (Subscriber)mockSubscriber.proxy() ); //注册订阅者,订阅者是一个Mock
  //这里比较难理解,mock.proxy()返回一个Object,然后转型为Subscriber。
 final String message = "message";  //要发布的消息。

Next we define expectations[期望结果] on the mock Subscriber that specify the methods that we expect to be called upon it during the test run.
我们定义那个在test运行期间被调用的订阅者的方法的期望结果。

We expect the receive method to be called with a single argument, the message that will be sent.
我们希望方法 receive 被调用并传递一个参数,也就是message,将要被发送

The eq method is defined in the MockObjectTestCase class and specifies a "constraint1" on the value of the argument passed to the subscriber: we expect the argument to be the equal to the message, but not necessarily the same object. (jMock provides several constraint types1 that can be used to precisely specify expected argument values).

We don't need to specify what will be returned from the receive method because it has a void return type.
我们不需要指定方法receive将会返回什么,因为它的返回类型是void
 [code]
 mockSubscriber.expects(once()).method("receive").with( eq(message) );
 [/code]
 
We then execute the code that we want to test.
接下来,我们执行我们要测试的代码。 
 publisher.publish(message);

After the test has finished, jMock will verify that the mock Subscriber was called as expected. If the expected calls were not made, the test will fail.

Here is the complete test.
[code]
 import org.jmock.*;

 class PublisherTest extends MockObjectTestCase {
  public void testOneSubscriberReceivesAMessage() {
   // set up
   Mock mockSubscriber = mock(Subscriber.class);
   Publisher publisher = new Publisher();
   publisher.add((Subscriber) mockSubscriber.proxy());
   
   final String message = "message";
   
   // expectations
   mockSubscriber.expects(once()).method("receive").with( eq(message) );
   
   // execute
   publisher.publish(message);
  }
 }
[/code]


2006-04-27 17:23:18
mockSubscriber.expects(once()).method("receive").with( eq(message) );
aPersonMock.expects(once()).method("getPerson").will(returnValue(new Person()));
个人理解,不一定正确:
with 表示 是传递参数
will 表示返回类型
will (后面一定要和returnValue相连)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值