Created
August 22, 2011 02:24
-
-
Save coderunner/1161530 to your computer and use it in GitHub Desktop.
Revisions
-
coderunner revised this gist
Aug 22, 2011 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,6 +37,8 @@ public class TestCurrentTimeFormatter public void testWithTestObject() { //Use mock or test object //Here a test object is used since we don't care if a call to time //accessor is made as long as the output string is all right. TimeAccessor accessor = TestObject.createTestObject(TimeAccessor.class); TestObject.Recorder<TimeAccessor> accessorRecorder = -
coderunner revised this gist
Aug 22, 2011 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,8 @@ public class CurrentTimeFormatter { private static String FORMAT = "Current time in millis is : "; //Not testable since can not predict value of //System.currentTimeMillis() public String formatCurrentTime_notTestable() { return FORMAT + System.currentTimeMillis(); @@ -36,11 +37,12 @@ public class TestCurrentTimeFormatter public void testWithTestObject() { //Use mock or test object TimeAccessor accessor = TestObject.createTestObject(TimeAccessor.class); TestObject.Recorder<TimeAccessor> accessorRecorder = new TestObject.Recorder<TimeAccessor>(accessor); accessorRecorder.record(accessor.getCurrentTime()) .andReturn(CURRENT_TIME); CurrentTimeFormatter formatter = new CurrentTimeFormatter(); -
coderunner revised this gist
Aug 22, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ public void testWithTestObject() CurrentTimeFormatter formatter = new CurrentTimeFormatter(); assertEquals("Current time in millis is : " + CURRENT_TIME, formatter.formatCurrentTime(accessor)); } @@ -60,7 +60,7 @@ long getCurrentTime() } }; assertEquals("Current time in millis is : " + CURRENT_TIME, formatter.formatCurrentTime()); } } -
coderunner revised this gist
Aug 22, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,7 +46,7 @@ public void testWithTestObject() assertEquals("Current time in millis is : "+CURRENT_TIME, formatter.formatCurrentTime(accessor)); } @Test public void testWithPackagePrivateMethod() -
coderunner created this gist
Aug 22, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ public class CurrentTimeFormatter { private static String FORMAT = "Current time in millis is : "; //Not testable since can not predict value of System.currentTimeMillis() public String formatCurrentTime_notTestable() { return FORMAT + System.currentTimeMillis(); } //Can mock TimeAccessor in test code. public String formatCurrentTime(TimeAccessor aTimeAccessor) { return FORMAT + aTimeAccessor.getCurrentTime(); } //Can override getCurrentTime in test code. public String formatCurrentTime() { return FORMAT + getCurrentTime(); } long getCurrentTime() { return System.currentTimeMillis(); } } /*** test code ***/ public class TestCurrentTimeFormatter { private static final long CURRENT_TIME = 1000l; @Test public void testWithTestObject() { //Use mock or test object TimeAccessor accessor = TestObject.createTestObject(TimeAccessor.class); TestObject.Recorder<TimeAccessor> accessorRecorder = new TestObject.Recorder<TimeAccessor>(accessor); accessorRecorder.record(accessor.getCurrentTime()).andReturn(CURRENT_TIME); CurrentTimeFormatter formatter = new CurrentTimeFormatter(); assertEquals("Current time in millis is : "+CURRENT_TIME, formatter.formatCurrentTime(accessor)); } @Test public void testWithPackagePrivateMethod() { //override non deterministic call CurrentTimeFormatter formatter = new CurrentTimeFormatter() { long getCurrentTime() { return CURRENT_TIME; } }; assertEquals("Current time in millis is : "+CURRENT_TIME, formatter.formatCurrentTime()); } }