【发布时间】:2016-09-08 02:12:02
【问题描述】:
在以下设置下
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class myTest {
我的 src 上有这个
DateFormat dataformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentdate = dataformat.format(new Date());
而且我知道 Date.getTime() 方法会被调用。
我在我的测试代码上试过这个。
final Date date = Mockito.mock(Date.class);
Mockito.when(date.getTime()).thenReturn(dateLongValue);
但是什么也没发生。 我也尝试过这样的模型
Mockito.when(date.format(new Date())).thenReturn(dateStringValue);
得到了
java.lang.NullPointerException
at java.text.DateFormat.format(DateFormat.java:346)
这可能很容易,但我没有运气。任何建议将不胜感激。
【问题讨论】:
-
我怀疑解决方案是不做
dataformat.format(new Date());。 -
检查下面的链接可能有用stackoverflow.com/questions/11887799/…
-
到底是什么问题?这两行代码都可以在您的测试中使用,实际上不需要模拟任何东西。如果您想检查这是否有效,只需解析返回 currentdate 并检查它是否在现在的几毫秒内......?你到底想测试什么?
标签: java junit mocking mockito simpledateformat