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/
分享到:
相关推荐
1.3 Example Real-Time Systems / 13 1.4 Common Misconceptions / 15 1.5 Brief History / 16 1.5.1 Theoretical Advances / 17 1.5.2 Early Systems / 17 1.5.3 Hardware Developments / 18 1.5.4 Early Software ...
golang-github-streadway-amqp-unit-test-devel-0-0.3.20190404git75d898a.el7.x86_64.rpm官方离线安装包,亲测可用
资源来自pypi官网。 资源全名:dbt-unit-test-0.0.2.tar.gz
中职英语教案unit5-it's-time-to-change.doc
How to Write a Unit Test Chapter 4. Tools for Testing Chapter 5. Test-Driven Development of an iOS App Chapter 6. The Data Model Chapter 7. Application Logic Chapter 8. Networking Code Chapter 9. ...
人教版必修3-Unit-4-How-Life-Began-on-the-Earth学案.doc
gwt-channel-api.zip,gwt中的gae通道api gwt中的gae通道api
Programmable Real-Time Unit (PRU):Extending Functionality of Existing SoCs
新目标-人教版-2013-新教材七-下unit2what-time-do-you-go-to-school单元检测.doc
四年级下册英语Unit2-What-time-is-it-教案.doc
八年级英语上册Unit 2 How often do you exercise-知识点归纳.docx
译林版英语四年级上册Unit-3-How-many-教案.docx
Unit-2-Ways--to-go-to-school-Part-B-课件.ppt
人教版课标小学英语四年级下册Unit2-What-time-is-it.doc
dao-unit-test.ftl
golang-github-go-ini-ini-unit-test-devel-1.39.3-0.1.gitf55231c.el7.x86_64.rpm 官方离线安装包,亲测可用
unit3-how-do-you-get-to-school-练习题.docx
人教版小学英语PEP三年级下册unit-6-How-many-教学设计说明.doc