使用 Java 8 时钟对类进行单元测试

编程入门 行业动态 更新时间:2024-10-22 07:37:51
本文介绍了使用 Java 8 时钟对类进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Java 8 引入了 java.time.Clock,它可以用作许多其他 java.time 对象的参数,允许您将真实或虚假的时钟注入他们.例如,我知道你可以创建一个 Clock.fixed() 然后调用 Instant.now(clock) 它会返回固定的 Instant> 你提供的.这听起来非常适合单元测试!

Java 8 introduced java.time.Clock which can be used as an argument to many other java.time objects, allowing you to inject a real or fake clock into them. For example, I know you can create a Clock.fixed() and then call Instant.now(clock) and it will return the fixed Instant you provided. This sounds perfect for unit testing!

但是,我无法弄清楚如何最好地使用它.我有一个类,类似于以下内容:

However, I'm having trouble figuring out how best to use this. I have a class, similar to the following:

public class MyClass { private Clock clock = Clock.systemUTC(); public void method1() { Instant now = Instant.now(clock); // Do something with 'now' } }

现在,我想对这段代码进行单元测试.我需要能够设置 clock 以产生固定时间,以便我可以在不同的时间测试 method().显然,我可以使用反射将 clock 成员设置为特定值,但如果我不必求助于反射就更好了.我可以创建一个公共的 setClock() 方法,但这感觉不对.我不想在方法中添加 Clock 参数,因为真正的代码不应该关心传入时钟.

Now, I want to unit test this code. I need to be able to set clock to produce fixed times so that I can test method() at different times. Clearly, I could use reflection to set the clock member to specific values, but it would be nice if I didn't have to resort to reflection. I could create a public setClock() method, but that feels wrong. I don't want to add a Clock argument to the method because the real code shouldn't be concerned with passing in a clock.

处理这个问题的最佳方法是什么?这是新代码,因此我可以重新组织课程.

What is the best approach for handling this? This is new code so I could reorganize the class.

澄清一下,我需要能够构造一个 MyClass 对象,但能够让该对象看到两个不同的时钟值(就好像它是一个常规的系统时钟一样).因此,我无法将固定时钟传递给构造函数.

To clarify, I need to be able to construct a single MyClass object but be able to have that one object see two different clock values (as if it were a regular system clock ticking along). As such, I cannot pass a fixed clock into the constructor.

推荐答案

让我把 Jon Skeet 的回答和评论放到代码中:

Let me put Jon Skeet's answer and the comments into code:

被测类:

public class Foo { private final Clock clock; public Foo(Clock clock) { this.clock = clock; } public void someMethod() { Instant now = clock.instant(); // this is changed to make test easier System.out.println(now); // Do something with 'now' } }

单元测试:

public class FooTest() { private Foo foo; private Clock mock; @Before public void setUp() { mock = mock(Clock.class); foo = new Foo(mock); } @Test public void ensureDifferentValuesWhenMockIsCalled() { Instant first = Instant.now(); // e.g. 12:00:00 Instant second = first.plusSeconds(1); // 12:00:01 Instant thirdAndAfter = second.plusSeconds(1); // 12:00:02 when(mock.instant()).thenReturn(first, second, thirdAndAfter); foo.someMethod(); // string of first foo.someMethod(); // string of second foo.someMethod(); // string of thirdAndAfter foo.someMethod(); // string of thirdAndAfter } }

更多推荐

使用 Java 8 时钟对类进行单元测试

本文发布于:2023-11-11 07:43:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时钟   单元测试   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!