[翻译]jMock Getting Started(jMock起步)

JMock 是一个使用mock对象进行java代码测试的程序库。Mock 对象用于设计和测试程序中不同对象间的交互作用。Jmock包的功能......

jMock Getting Started(jMock起步)

 

原文(英文)网址:http://jmock.org/getting-started.html

翻译:陈海青(http://www.chq.name

2006.08.24
 

About jMock(关于jMock)

jMock is a library for testing Java code using mock objects1.

Mock objects help you design and test the interactions between the objects in your programs.

The jMock package:

  • makes it quick and easy to define mock objects, so you don't break the rhythm of programming.
  • lets you define flexible constraints over object interactions, reducing the brittleness of your tests.
  • is easy to extend.

JMock 是一个使用mock对象进行java代码测试的程序库。Mock 对象用于设计和测试程序中不同对象间的交互作用。Jmock包的功能:

  • 轻松快速地定义mock 对象,不会影响正常速度。
  • 可以自由定义对象间的交互关系,减少测试的脆弱性。
  • 易于扩展.

Getting started(起步)

This guide assumes you are familiar with unit-testing and JUnit3.

For a simple example we are going to test a publish/subscribe message system. A Publisher sends objects to zero or more Subscribers. We want to test the Publisher, which involves testing its interactions with its Subscribers.

The Subscriber interface looks like this:

本手册假定你了解单元测试和Junit。作为一个简单的例子,我们打算测试一个出版/订阅信息系统。出版者发送对象给零个或多个订阅者。我们来测试这个出版者,包括与订阅者之间的交互。

订阅者接口如下所示:

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.

我们将测试出版者发送一个信息给一个注册的订阅者。为测试他们之间的交互关系,我们使用了一个虚拟(mock)的订阅者对象

首先,我们必须import jMock classes,定义测试设备,定义测试用例方法:

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.

现在,要写testOneSubscriberReceivesAMessage方法。

首先要设置测试用的context,建立一个测试用的出版者,建议mock的订阅者来接受信息,然后,把订阅者注册到出版者,最后建立一个用于出版的信息对象。

Mock mockSubscriber = mock(Subscriber.class);

Publisher publisher = new Publisher();

publisher.add( (Subscriber)mockSubscriber.proxy() );

 

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. We expect the receive method to be called with a single argument, the message that will be sent. 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.

下一步,给mock订阅者定义一个期望值(expectations),来指定在测试过程中我们期望被调用的的方法。我们希望receive方法被使用单一参数来调用,从而使信息被发送出去。这里的eq方法在MockObjectTestCase中被定义,并被指定使用一个约束("constraint1")来作用到传送到订阅者的参数上:期望这个参数等同于message对象,但不必是同一个对象。(jMock 提供了可用于指定适合的不同期望参数值的多个约束类型)我们不必制定receive方法的返回值,因为它返回的是空值(void).

mockSubscriber.expects(once()).method("receive").with( eq(message) );

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.

测试结束后,jMock将校验mock订阅者是否象期望的那样被调用,如果没有,测试失败。以下是完整的测试代码。

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);

    }

}

That concludes this quick introduction. More advanced topics are covered in other tutorials2.

快速介绍到此结束,更多内容参加其他教程(other tutorials2)。

Links(相关链接):

1. http://www.jmock.org/constraints.html

2. http://www.jmock.org/docs.html#tutorials

3. http://junit.org/

4.  http://www.chq.name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值