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

本文介绍了如何使用jMock库进行Java代码测试,包括定义mock对象、设置期望值和执行测试。通过实例展示了如何测试一个发布/订阅消息系统,确保了订阅者正确接收消息。

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

在自媒体领域,内容生产效率与作品专业水准日益成为从业者的核心关切。近期推出的Coze工作流集成方案,为内容生产者构建了一套系统化、模块化的创作支持体系。该方案通过预先设计的流程模块,贯穿选题构思、素材整理、文本撰写、视觉编排及渠道分发的完整周期,显著增强了自媒体工作的规范性与产出速率。 经过多轮实践验证,这些标准化流程不仅精简了操作步骤,减少了机械性任务的比重,还借助统一的操作框架有效控制了人为失误。由此,创作者得以将主要资源集中于内容创新与深度拓展,而非消耗于日常执行事务。具体而言,在选题环节,系统依据实时舆情数据与受众偏好模型生成热点建议,辅助快速定位创作方向;在编辑阶段,则提供多套经过验证的版式方案与视觉组件,保障内容呈现兼具美学价值与阅读流畅性。 分发推广模块同样经过周密设计,整合了跨平台传播策略与效果监测工具,涵盖社交网络运营、搜索排序优化、定向推送等多重手段,旨在帮助内容突破单一渠道局限,实现更广泛的受众触达。 该集成方案在提供成熟模板的同时,保留了充分的定制空间,允许用户根据自身创作特性与阶段目标调整流程细节。这种“框架统一、细节可变”的设计哲学,兼顾了行业通用标准与个体工作习惯,提升了工具在不同应用场景中的适应性。 从行业视角观察,此方案的问世恰逢其时,回应了自媒体专业化进程中对于流程优化工具的迫切需求。其价值不仅体现在即时的效率提升,更在于构建了一个可持续迭代的创作支持生态。通过持续吸纳用户反馈与行业趋势,系统将不断演进,助力从业者保持与行业发展同步,实现创作质量与运营效能的双重进阶。 总体而言,这一工作流集成方案的引入,标志着自媒体创作方法向系统化、精细化方向的重要转变。它在提升作业效率的同时,通过结构化的工作方法强化了内容产出的专业度与可持续性,为从业者的职业化发展提供了坚实的方法论基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值