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/
分享到:
相关推荐
《PyPI官网下载的dbt-unit-test-0.0.2.tar.gz——Python数据库测试框架解析》 在Python的世界里,PyPI(Python Package Index)是开发者们分享和获取开源软件包的重要平台。本文将围绕从PyPI官网下载的"dbt-unit-...
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. ...
golang-github-streadway-amqp-unit-test-devel-0-0.3.20190404git75d898a.el7.x86_64.rpm官方离线安装包,亲测可用
人教版必修3-Unit-4-How-Life-Began-on-the-Earth学案.doc
译林版英语四年级上册Unit-3-How-many-教案.docx
人教版课标小学英语四年级下册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 官方离线安装包,亲测可用
官方离线安装包,亲测可用
golang-github-davecgh-go-spew-unit-test-0-0.11.git6d21280.1.el7.x86_64.rpm官方离线安装包,亲测可用
Use of the Pentaho checkstyle format (via mvn checkstyle:check and reviewing the report) and developing working Unit Tests helps to ensure that pull requests for bugs and improvements are processed ...
官方离线安装包,亲测可用
官方离线安装包,亲测可用
这个“EMOCA-test-example-data.zip”文件包含了EMOCA算法应用的示例数据,可以帮助开发者或研究人员了解如何使用该技术进行实际操作。 在EMOCA算法中,主要涉及以下几个关键知识点: 1. **面部动作编码(FACS)**...
This fully revised and popular book is now up-to-date and even more comprehensive than before. The Indispensable PC Hardware Book 4/e will be 'indispensable' to anyone who wants to know more about ...
We'll look at how to use the unit test framework called Ceedling to help us do this. In the first example, we'll see how to create tests and write the code to make them pass. In the second example we ...
This book is intended for Python developers who want to use the principles of test-driven development (TDD) to create efficient and robust applications. In order to get the best out of this book, you ...
官方离线安装包,亲测可用