如何使用mock对象模仿日常程序?

发布于 2024-07-26 19:53:54 字数 348 浏览 12 评论 0原文

我的程序有一个日常例程,类似于闹钟事件。 比如说,当下午 2 点(时间是我电脑中的系统时间)时,为我做点什么。

我想做的是加快测试周期(我真的不想等 4 天查看日常工作并检查错误。)我在 Mock object的wiki,作者确实提到了闹钟程序。 我很高兴看到,但仍然不知道该怎么做。

我是 Mock Object 的新手,并且正在使用 Java 进行编程。 所以 JMock 或 EasyMock(或任何类似的)对我来说可能没问题。

谢谢

My program has a daily routine, similar to an alarm clock event. Say, when it's 2pm(The time is the system time in my pc), do something for me.

What I want to do is to speed up the testing period(I don't really want to wait 4 days looking at the daily routine and check errors.) I read on wiki of Mock object, the writer DID mention alarm clock program. I was so happy to see but still, don't know how to do it.

I am new to Mock Object, and I am programming in Java. So JMock or EasyMock(or any similar) might okay to me.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抱着落日 2024-08-02 19:53:54

每当您需要获取当前时间时,不要直接使用系统时钟 - 使用如下接口:

public interface Clock
{
    long currentMillis();
}

然后您可以使用系统时钟来实现它以进行生产,并传递一个假值进行测试,您可以在其中设置假值到任何你想要的时间。

但是,您还需要模拟驱动系统的任何内容 - 您是否明确等待特定时间,或者是否有其他东西调用您的代码?

Whenever you need to get the current time, don't use the system clock directly - use an interface such as:

public interface Clock
{
    long currentMillis();
}

You can then implement this with a system clock for production, and pass in a fake for tests, where you can set the fake to whatever time you want.

However, you'll also need to mock out whatever's driving your system - are you explicitly waiting for a particular time, or does something else call your code?

战皆罪 2024-08-02 19:53:54

我确实必须道歉,因为您询问了有关 java 的问题,而我出去吃午餐时谈到了 java,但一种解决方案是模拟 DateTime 对象并将其设置为所需的时间。

在 .NET 中,它看起来像这样:

public static class SystemTime
{
    public static Func<DateTime> Now = () => DateTime.Now;
}

SystemTime.Now = () => new DateTime(2000,1,1);

来自: 处理测试中的时间


... [A]n 闹钟程序会导致
在某个时间响铃可能
从外部获取当前时间
世界。 为了测试这一点,测试必须
等到闹钟响的时候才知道
是否已按响门铃
正确。 如果使用模拟对象
真实物体的位置,可以是
编程以提供铃声
时间(是否真的是那个时间
或不)这样闹钟
程序可以单独测试。

您引用的这个闹钟给出了模拟对象的示例。 它实际上并不是一个可以从模拟框架中使用的对象。

I do have to apologize since you've asked about java and I'm out to lunch when it comes to java, but one solution is to Mock the DateTime object and set it for the desired time.

In .NET it would look something like this:

public static class SystemTime
{
    public static Func<DateTime> Now = () => DateTime.Now;
}

SystemTime.Now = () => new DateTime(2000,1,1);

From: Dealing With Time In Tests


... [A]n alarm clock program which causes a
bell to ring at a certain time might
get the current time from the outside
world. To test this, the test must
wait until the alarm time to know
whether it has rung the bell
correctly. If a mock object is used in
place of the real object, it can be
programmed to provide the bell-ringing
time (whether it is actually that time
or not) so that the alarm clock
program can be tested in isolation.

This Alarm Clock you're referencing is giving an example of mocking an object. It isn't actually an object you can use from the mock framework.

长途伴 2024-08-02 19:53:54

基本上,您在测试中所做的就是伪造时钟事件。 确切的方式在某种程度上取决于设计以及您如何等待事件,但为了保持简单(如果不是纯粹),我的方法是有一个在触发时间事件时调用的方法,然后测试双方的。

首先是测试调用该方法并查看定时事件是否符合您的预期。 然后模拟该类(使用 JMock 的首选方法是使其成为一个接口并让您的调用方法实现该接口)。

然后,您将模拟传递给处理计时的类。 在这里,我再次抽象出线程/触发问题(例如检查系统时钟和启动线程),并让模拟返回值本质上立即运行事件。

然后将实际读取系统时钟并启动线程的实际代码保持尽可能小,这将是不进行单元测试的区域。

Basically what you do in the test is fake the clock events. Exactly how depends somewhat on the design, and how you are waiting for the event, but to keep it simple (if not pure) my approach would be to have a method that is called when the time event is triggered, and then test both sides of that.

First is to test calling the method and seeing that the timed event does what you expect. Then mock that class (with JMock the preferred way is to make it an interface and have your calling method implement that interface).

You then pass the mock to the class that handles the timing. Here again, I would abstract out the threading/triggering issues (such as checking the system clock and starting the thread) and have mocks return values that essentially run the event right away.

Then keep the real code that actually reads the system clock and starts the thread as small as possible, and that will be the area that isn't under unit test.

稳稳的幸福 2024-08-02 19:53:54

模拟与单元测试相关。 对于您描述的功能,我会将触发器(在您的情况下是闹钟事件)与流程(无论您的“日常工作”正在做什么)分开,并对流程功能进行单元测试。

然后将您的注意力转移到将调用流程功能的调度代码上。 我建议使用诸如 Quartz 之类的东西,但如果你要扮演你的角色-拥有单元测试,因为它可以与实际系统时钟一起工作,前提是您根据系统时钟的当前值分配触发时间,因为您将测试的只是触发事件发生。

Mocking relates to unit testing. For the functionality you are describing I would separate the trigger (in your case the alarm clock event) from the process (whatever it is your 'daily routine' is doing) and unit test the process functionality.

Then direct your attention to the scheduling code that will invoke your process functionality. I would recommend using something like Quartz for this but if you are going to role-your-own the unit test for it can work with the actual system clock provided you assign the trigger time based on the current value of the system clock as all you will be testing for is that the trigger event occurs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文