[테스트 코드 리팩토링] extract method
@Test
public void getAttendanceCountOf() throws Exception {
insertXmlData("testData.xml");
Member member = new Member();
Study study = new Study();
member.setId(1);
study.setId(1);
assertThat(sr.getAttendanceCountOf(member, study), is(2));
member.setId(1);
study.setId(2);
assertThat(sr.getAttendanceCountOf(member, study), is(1));
member.setId(2);
study.setId(2);
assertThat(sr.getAttendanceCountOf(member, study), is(1));
}
다음과 같이 코드를 리팩토링할 수 있습니다.
@Test
public void getAttendanceCountOf() throws Exception {
insertXmlData("testData.xml");
checkAttendanceCountOf(1, 1, 2);
checkAttendanceCountOf(1, 2, 1);
checkAttendanceCountOf(2, 2, 1);
}
private void checkAttendanceCountOf(int memberId, int studyId, int count){
Member member = new Member();
Study study = new Study();
member.setId(memberId);
study.setId(studyId);
assertThat(sr.getAttendanceCountOf(member, study), is(count));
}
이번 경우에는 코드 라인수 차이가 얼마 나지 않지만, 대부분의 경우 훨씬 깔끔해집니다.