Skip to content

Instantly share code, notes, and snippets.

@coderunner
Created August 22, 2011 02:24
Show Gist options
  • Select an option

  • Save coderunner/1161530 to your computer and use it in GitHub Desktop.

Select an option

Save coderunner/1161530 to your computer and use it in GitHub Desktop.

Revisions

  1. coderunner revised this gist Aug 22, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.java
    Original 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 =
  2. coderunner revised this gist Aug 22, 2011. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions gistfile1.java
    Original 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()
    //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);
    TimeAccessor accessor =
    TestObject.createTestObject(TimeAccessor.class);
    TestObject.Recorder<TimeAccessor> accessorRecorder =
    new TestObject.Recorder<TimeAccessor>(accessor);

    accessorRecorder.record(accessor.getCurrentTime()).andReturn(CURRENT_TIME);
    new TestObject.Recorder<TimeAccessor>(accessor);
    accessorRecorder.record(accessor.getCurrentTime())
    .andReturn(CURRENT_TIME);

    CurrentTimeFormatter formatter = new CurrentTimeFormatter();

  3. coderunner revised this gist Aug 22, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.java
    Original 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,
    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,
    assertEquals("Current time in millis is : " + CURRENT_TIME,
    formatter.formatCurrentTime());
    }
    }
  4. coderunner revised this gist Aug 22, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.java
    Original 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()
  5. coderunner created this gist Aug 22, 2011.
    66 changes: 66 additions & 0 deletions gistfile1.java
    Original 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());
    }
    }