While we are writing the unit test cases, we may meet some method that is real-time based. It'll be a challenge to get the expected result as they are changing with time, then it's time to use the Mock tool.
today I meet the same issue, I need to test one method timeFormat(), this method is to format the current time with the formate provided, such as "yyyyMMddhhmmss", the result for current time should be "20130530041043". It takes me long time to solve the real-time issue, because the timeFormat() will always call new Date(), but I cannot handle the result obviously. What I am interested is whether the method can format the time with the given time. Then the "Power Mock" gave me a big help, the following is my resolution:
the businees code is :
import java.text.SimpleDateFormat;
import java.util.Date;
public class Timer {
public String timeFormat(){
String format = "yyyyMMddhhmmss";
SimpleDateFormat dateFm = new SimpleDateFormat(format);
Date date = new Date();
long t = date.getTime();
String dateTime = dateFm.format(new Date(t));
return dateTime;
}
...//other businees code
}
of course, if every time I call new Date(), and it return with a fixed value, it will be very easy to test this method. OK, this is the point we need to face. With PowerMock, we can resolve it very easily.
@RunWith(PowerMockRunner.class)
@PrepareForTest({Timer.class})
public class TimerTest {
Timer timer;
@Before
public void init() throws Exception{
timer = new Timer();
}
@Test
public void testTimeFormat () throws Exception{
long simulatedTime = 1369897050940l;//This the simulated value we used.
String expectResult = "20130530033718";//simulated.
Date date = PowerMock.createMock(Date.class);
expectNew(Date.class).andReturn(date);
expect(date.getTime()).andReturn(simulatedTime);
// we hope the new Date() will return with the date we have defined //before. (which means the new Date() will be a fixed value)
// we need to simulate getTime() method because the format() method will call it.
PowerMock.replay(date,Date.class);
String actualResult = timer.timeFormat();
assertEquals(expectResult,actualResult);
}
}
I have added the detail comments in the code. In this way, we can simulate the Object new Date() with any value.
you can refer to this page to have a detail knowledge about powermock:
http://www.ibm.com/developerworks/cn/java/j-lo-powermock/
分享到:
相关推荐
For example, if the clustered index is on (lastname, firstname) and a nonclustered index is on firstname, the firstname value will not be duplicated in the nonclustered index leaf rows. Note The ...
for example(2KB)<END><br>87,VBAutoIncrement_src.zip An article on how to add an automatic build incrementer to VC6(2KB)<END><br>88,MakeCodeNicer.zip A great macro for reformatting C++ source ...
wangtengfei-hn_EmployeesExample_23540_1745868671962
scratch少儿编程逻辑思维游戏源码-汽车冲突.zip
scratch少儿编程逻辑思维游戏源码-棱镜.zip
少儿编程scratch项目源代码文件案例素材-直升机坠毁.zip
输入法优化与定制_五笔编码编辑与词库管理_Rime输入法引擎与86极点码表_跨平台五笔码表编辑器工具_for_macOS与Windows系统_支持用户自定义词条添加删除与排序_提供
少儿编程scratch项目源代码文件案例素材-主题乐园大亨.zip
scratch少儿编程逻辑思维游戏源码-迷失在像素平原.zip
少儿编程scratch项目源代码文件案例素材-纸格通关 云变量.zip
wanjunshe_Python-Tensorflow_12888_1745868924470
scratch少儿编程逻辑思维游戏源码-深入海底.zip
驾校自动化_网页自动化爬虫技术_Python27多线程HTTP请求模拟_龙泉驾校2014版约车系统自动预约助手_通过模拟登录和循环请求实现自动约车功能_支持失败自动递增车号重试_
scratch少儿编程逻辑思维游戏源码-南瓜危机.zip
scratch少儿编程逻辑思维游戏源码-皮博冒险者.zip
基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档~ 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档 基于c++开发的网络嗅探器,重点对TCP、UDP、ARP、IGMP、ICMP 等数据包进行分析,实现捕捉前过滤、数据包统计、流量统计等功能+源码+项目文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用,详情见md文档
用于释放电脑的内存,很好用。
scratch少儿编程逻辑思维游戏源码-气球足球.zip
ollama 0.6.6.0官网下载,不方便的可以从这里下载
scratch少儿编程逻辑思维游戏源码-魔幻之塔.zip