[翻译]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

在充满仪式感的生活里,一款能传递心意的小工具总能带来意外惊喜。这款基于Java开发的满屏飘字弹幕工具,正是为热爱生活、乐于分享的你而来——它以简洁优雅的视觉效果,将治愈系文字化作灵动弹幕,在屏幕上缓缓流淌,既可以作为送给心仪之人的浪漫彩蛋,也能成为日常自娱自乐、舒缓心情的小确幸。 作为程序员献给crush的心意之作,工具的设计藏满了细节巧思。开发者基于Swing框架构建图形界面,实现了无边框全屏显示效果,搭配毛玻璃质感的弹幕窗口与圆润边角设计,让文字呈现既柔和又不突兀。弹幕内容精选了30条治愈系文案,从“秋天的风很温柔”到“你值得所有温柔”,涵盖生活感悟、自我关怀、浪漫告白等多个维度,每一条都能传递温暖力量;同时支持自定义修改文案库,你可以替换成专属情话、纪念文字或趣味梗,让弹幕更具个性化。 在视觉体验上,工具采用柔和色调生成算法,每一条弹幕都拥有独特的清新配色,搭配半透明渐变效果与平滑的移动动画,既不会遮挡屏幕内容,又能营造出灵动治愈的氛围。开发者还优化了弹幕的生成逻辑,支持自定义窗口大小、移动速度、生成间隔等参数,最多可同时显示60条弹幕,且不会造成电脑卡顿;按下任意按键即可快速关闭程序,操作便捷无负担。 对于Java学习者而言,这款工具更是一份优质的实战参考。源码完整展示了Swing图形界面开发、定时器调度、动画绘制、颜色算法等核心技术,注释清晰、结构简洁,哪怕是初学者也能轻松理解。开发者在AI辅助的基础上,反复调试优化细节,解决了透明度控制、弹幕碰撞、资源占用等多个问题,这份“踩坑实录”也为同类项目开发提供了宝贵经验。 无论是想给喜欢的人制造浪漫惊喜,用满屏文字传递心意;还是想在工作间隙用治愈文案舒缓压力,或是作为Java学习的实战案例参考,这款满屏飘字弹幕工具都能满足你的需求。它没有复杂的操作流程,无需额外配置环境,下载即可运行,用最纯粹的设计传递最真挚的
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值