Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
좋은 가독성을 가지는 테스트를 만들자
import org.hamcrest.*;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import static com.coupang.job.worker.virtualaccount.TimeMachineTest.DateFormatMatcher.formmated;
import static java.util.Calendar.*;
import static java.util.Calendar.MILLISECOND;
import static java.util.Calendar.SECOND;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
//테스트케이스가 가 유사한 것들이 왜 반복적으로 만들었는지에 대해서는 넘어가주시길 단순하게 시간에 대한 테스트를 할때 방법에 대한 부분만 보아주실길 바랍니다.
public class TimeMachineTest {
TimeMachine sut= new TimeMachine();
@Test
public void 두시간뒤로시간을돌린다(){
Calendar cal = Calendar.getInstance();
cal.set(YEAR, 2014);
cal.set(MONTH, MARCH);
cal.set(DATE, 1);
cal.set(HOUR_OF_DAY, 5);
cal.set(MINUTE, 0);
cal.set(SECOND, 0);
cal.set(MILLISECOND, 0);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(sut.두시간_뒤로_돌린다(cal.getTime()));
assertThat(cal2.get(MONTH), is(MARCH));
assertThat(cal2.get(DATE), is(1));
assertThat(cal2.get(HOUR_OF_DAY), is(3));
assertThat(cal2.get(MINUTE), is(0));
assertThat(cal2.get(SECOND), is(0));
assertThat(cal2.get(MILLISECOND), is(0));
// 한글메서드가 두시간뒤라고하니 그렇다 치고(한글메서드를 왜 사용했냐는 둥 딴지는 걸지 말아주세요 ㅜㅜ) 테스트 Actual Data 와 Expected Data 의 비교 단정문이 눈에 들어오는가?
}
@Test
public void 세시간뒤로시간을돌린다() throws Exception{
Calendar cal2 = Calendar.getInstance();
cal2.setTime(sut.세시간_뒤로_돌린다(_2014년_3월_1일_오전_05시()));
assertThat(cal2.get(MONTH), is(MARCH));
assertThat(cal2.get(DATE), is(1));
assertThat(cal2.get(HOUR_OF_DAY), is(2));
assertThat(cal2.get(MINUTE), is(0));
assertThat(cal2.get(SECOND), is(0));
assertThat(cal2.get(MILLISECOND), is(0));
}
private Date _2014년_3월_1일_오전_05시() {
Calendar cal = Calendar.getInstance();
cal.set(YEAR, 2014);
cal.set(MONTH, MARCH);
cal.set(DATE, 1);
cal.set(HOUR_OF_DAY, 5);
cal.set(MINUTE, 0);
cal.set(SECOND, 0);
cal.set(MILLISECOND, 0);
return cal.getTime();
}
@Test
public void 네시간뒤로시간을돌린다() throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
assertThat(sdf.format(_2014년_3월_1일_오전_05시()),is("2014-03-01 05:00"));
assertThat(sdf.format(sut.네시간_뒤로_돌린다(_2014년_3월_1일_오전_05시())), is("2014-03-01 01:00"));
}
SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date _2014년_3월_1일_5시정각 = 시간(2014, 2, 1, 5, 0);
@Test
public void 다섯시간뒤로시간을돌린다() throws Exception{
assertThat(SDF.format(sut.다섯시간_뒤로_돌린다(_2014년_3월_1일_5시정각)),is("2014-03-01 00:00"));
}
private Date 시간(int year,int month,int date, int hour, int minite) {
Calendar cal = Calendar.getInstance();
cal.set(YEAR, year);
cal.set(MONTH, month);
cal.set(DATE, date);
cal.set(HOUR_OF_DAY, hour);
cal.set(MINUTE, minite);
cal.set(SECOND, 0);
cal.set(MILLISECOND, 0);
return cal.getTime();
}
@Test
public void 시간을돌린다() throws Exception{
assertEquals("2014-03-01 06:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, -1));
assertEquals("2014-03-01 05:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, 0));
assertEquals("2014-03-01 04:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, 1));
assertEquals("2014-03-01 03:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, 2));
assertEquals("2014-03-01 02:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, 3));
assertEquals("2014-03-01 01:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, 4));
assertEquals("2014-03-01 00:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, 5));
assertEquals("2014-02-28 23:00", sut.시간을_돌린다(_2014년_3월_1일_5시정각, 6));
}
private void assertEquals(String expected, Date date) {
assertThat(SDF.format(date),is(expected));
}
@Test
public void 시간을돌린다2() throws Exception{
assertThat(sut.시간을_돌린다(_2014년_3월_1일_5시정각, -1),is(formmated("2014-03-01 06:00","yyyy-MM-dd HH:mm")));
assertThat(sut.시간을_돌린다(_2014년_3월_1일_5시정각, 0),is(formmated("2014-03-01 05:00","yyyy-MM-dd HH:mm")));
assertThat(sut.시간을_돌린다(_2014년_3월_1일_5시정각, 1),is(formmated("2014-03-01 04:00","yyyy-MM-dd HH:mm")));
assertThat(sut.시간을_돌린다(_2014년_3월_1일_5시정각, 2),is(formmated("2014-03-01 03:00","yyyy-MM-dd HH:mm")));
assertThat(sut.시간을_돌린다(_2014년_3월_1일_5시정각, 3),is(formmated("2014-03-01 02:00","yyyy-MM-dd HH:mm")));
assertThat(sut.시간을_돌린다(_2014년_3월_1일_5시정각, 4),is(formmated("2014-03-01 01:00","yyyy-MM-dd HH:mm")));
assertThat(sut.시간을_돌린다(_2014년_3월_1일_5시정각, 5),is(formmated("2014-03-01 00:00","yyyy-MM-dd HH:mm")));
}
public static class DateFormatMatcher extends TypeSafeDiagnosingMatcher<Date>{
private final SimpleDateFormat sdf;
private final String expected;
public DateFormatMatcher(String format, String expected) {
this.sdf =new SimpleDateFormat(format);
this.expected = expected;
}
@Override
public void describeTo(Description description) {
description.appendText(expected);
}
@Override
protected boolean matchesSafely(Date item, Description mismatchDescription) {
mismatchDescription.appendText(sdf.format(item));
return sdf.format(item).equals(expected);
}
@Factory
public static Matcher<Date> formmated(String expected,String format){
return new DateFormatMatcher(format,expected);
}
}
//이렇게 까지 해야하는가 에 대해서 답변하자면
//이렇게 까지 할 필요 없다 단지 처음 알아보기 힘든 테스트코드는 추후 테스트를 무디게 하고 읽기 싫어지는 테스트가 되어 의미가 감가상각 된다는 점을 이야기하고 싶다.
public static class TimeMachine{
Date 두시간_뒤로_돌린다(Date date){
return 시간을_돌린다(date, 2);
}
Date 세시간_뒤로_돌린다(Date date){
return 시간을_돌린다(date, 3);
}
Date 네시간_뒤로_돌린다(Date date){
return 시간을_돌린다(date, 4);
}
Date 다섯시간_뒤로_돌린다(Date date){
return 시간을_돌린다(date, 5);
}
Date 시간을_돌린다(Date date, int amount) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(HOUR_OF_DAY, -amount);
return cal.getTime();
}
}
}
@yangwansu

This comment has been minimized.

Copy link
Owner Author

@yangwansu yangwansu commented Mar 29, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment