有没有办法在scala中模拟Instant.now?
下面的代码给了我一个问题,因为Instant.now方法是static val instant: Instant = mockInstant when(instant.now)...
发布于 2017-01-21 02:03:57
您可以通过执行以下操作来模拟“伴生对象”(引用,因为Instant来自Java):
mock[Instant.type]而不是
mock[Instant]发布于 2017-01-21 02:52:05
模仿静态方法并不是很直接。不过,有几种方法可以做到这一点:
someObject.now()包装函数,而不是在代码中直接调用Instance.now()。然后,您可以在someObject上模拟now()方法,就像一个普通的模拟。如果你能分享更多细节,我们可能会对解决方案有更好的想法。
https://stackoverflow.com/questions/41769654
复制相似问题