-
-
Save anonymous/56efd087f3d52afd167e to your computer and use it in GitHub Desktop.
StefMa @Medium - Developers, please write tests!
This file contains 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 characters
public class MyClock { | |
public String getCurrentTime(TimeFormat format) { | |
switch (format) { | |
case GERMAN: | |
return getStringFormattedTimeWithPatternAndLocale("HH:mm 'Uhr'", Locale.GERMANY); | |
case AMERICAN: | |
return getStringFormattedTimeWithPatternAndLocale("hh:mm a", Locale.US); | |
case GERMAN_WITH_DATE: | |
return getStringFormattedTimeWithPatternAndLocale("HH:mm 'Uhr,' EEEE dd.MM.yyyy", Locale.GERMANY); | |
case AMERICAN_WITH_DATE: | |
return getStringFormattedTimeWithPatternAndLocale("hh:mm a',' EEEE MM.dd.yyyy", Locale.US); | |
default: | |
return ""; | |
} | |
} | |
@VisibleForTesting | |
long currentTimeMillis() { | |
return System.currentTimeMillis(); | |
} | |
private String getStringFormattedTimeWithPatternAndLocale(String pattern, Locale locale) { | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale); | |
Date currentDate = new Date(currentTimeMillis()); | |
return simpleDateFormat.format(currentDate); | |
} | |
} |
This file contains 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 characters
public class MyClockTest { | |
private MyClock mMyClock; | |
private long mTimeMillis = System.currentTimeMillis(); | |
@Before | |
public void setUp() throws Exception { | |
mMyClock = new MyClock() { | |
long currentTimeMillis() { | |
return mTimeMillis; | |
} | |
}; | |
} | |
@Test | |
public void testGetCurrentName_ShouldReturnTimeInGermanFormat() throws Exception { | |
String germanTime = mMyClock.getCurrentTime(TimeFormat.GERMAN); | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm 'Uhr'", Locale.GERMANY); | |
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis)); | |
assertThat(germanTime, is(equalTo(formattedTime))); | |
} | |
@Test | |
public void testGetCurrentName_ShouldReturnTimeInAmericanFormat() throws Exception { | |
String americanTime = mMyClock.getCurrentTime(TimeFormat.AMERICAN); | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a", Locale.US); | |
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis)); | |
assertThat(americanTime, is(equalTo(formattedTime))); | |
} | |
@Test | |
public void testGetCurrentName_ShouldReturnTimeInGermanFormatWithDate() throws Exception { | |
String americanTime = mMyClock.getCurrentTime(TimeFormat.GERMAN_WITH_DATE); | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm 'Uhr,' EEEE dd.MM.yyyy", Locale.GERMANY); | |
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis)); | |
assertThat(americanTime, is(equalTo(formattedTime))); | |
} | |
@Test | |
public void testGetCurrentName_ShouldReturnTimeInAmericanFormatWithDate() throws Exception { | |
String americanTime = mMyClock.getCurrentTime(TimeFormat.AMERICAN_WITH_DATE); | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a',' EEEE MM.dd.yyyy", Locale.US); | |
String formattedTime = simpleDateFormat.format(new Date(mTimeMillis)); | |
assertThat(americanTime, is(equalTo(formattedTime))); | |
} | |
@Test | |
public void testCurrentTimeMillis_ShouldReturnCurrentTimeMillis() throws Exception { | |
long clockTime = mMyClock.currentTimeMillis(); | |
assertThat(mTimeMillis, is(equalTo(clockTime))); | |
} | |
} |
This file contains 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 characters
public enum TimeFormat { | |
GERMAN, AMERICAN, GERMAN_WITH_DATE, AMERICAN_WITH_DATE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment