단위 테스트 (UnitTest)전체 기능이 아닌 일부 범위만 테스트주로 클래스의 메서드가단위 테스트의 대상단위 테스트 예,DB 연동 처리(DAO) 클래스의 select() 메서드 테스트회원 가입 Service 클래스의 regist() 메서드 테스트ControllerServiceDAO통합테스트 (Integration Test)단위테스트 (Unit Test)
18.
단위 테스트 프레임워크기본프레임워크JUnit (*)TestNGJUnit테스트 예public class CryptoTest { @Test public void test() { String source = "madvirus,최범균,전략기술팀"; String encrypted = Crypto.encrypt(source); String expectedEnc = "UiDWVjs050cbZpQDOPV…..";assertEquals(expectedEnc, encrypted);assertFalse(source.equals(encrypted));System.out.println(encrypted); String decrypted = Crypto.decrypt(encrypted);assertEquals(source, decrypted); }}
인터페이스에 대고 개발publicclass Service { private Dao dao; public intsomeMethod() {intval = dao.select(); return val + 1; } public void setDao(Dao dao) { this.dao = dao; }}public interface Dao {int select();}
24.
의존 대상을 Mock으로대체Mock을 이용한 단위 테스트실제 구현 대신 Mock 구현 사용예,public class ServiceTest { @Test public void testSomeMethod() {MockDaodao = new MockDao(); Service service = new Service();service.setDao(mockDao);int result = service.someMethod();assertEquals(2, result); }}public class MockDao implements Dao { public int select() { return 1; }}
25.
Mock 생성Mock을 생성해주는라이브러리Mockito, Spring Test, easyMock, jMock등Mock 이용 테스트 샘플public void setUp() {authenticator = mock(Authenticator.class); // mock 생성loginController = new LoginController();loginController.setAuthenticator(authenticator);}@Testpublic void login() {HttpServletRequestrequest = new MockHttpServletRequest(); // 웹 요청 Mock Authentication authentication = mock(Authentication.class);when(authenticator.authenticate(Mockito.<AuthenticationRequest> anyObject())) .thenReturn(authentication);ModelAndViewmav = loginController.login(request, "id", "password"); // 테스트assertEquals(LoginController.DEFAULT_SUCCESS_VIEW_NAME, mav.getViewName());verify(authenticator).authenticate(Mockito.<AuthenticationRequest>anyObject());}
26.
내용 정리테스트가 용이하도록테스트하기쉬운 크기로 클래스를 잘게 나눔인터페이스에 의존해서 테스트 용이하도록,Mock을 이용한 테스트덤으로 병행 개발 가능테스트 프레임워크를 사용JUnit, Mockito, Spring Test짧은 주기로 리듬감 있는 개발 실현!
27.
앞으로…. 숙제는단위 테스트습득JUnit세미나통합 테스트 기법 습득Spring이 제공하는 테스트 기법DB 테스트 기법 습득DbUnit세미나웹 기능 자체의 테스트 자동화Selenium 세미나