문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
java:spock [2016/07/27 12:31] kwon37xi |
java:spock [2020/06/26 15:20] kwon37xi [Spock 1.3 & groovy 2.5 문제점] |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Spock ====== | ====== Spock ====== | ||
* [[https:// | * [[https:// | ||
+ | * Test 코드가 설계 명세서 역할을 할 수 있을 정도로 가독성 높은 테스트를 짤 수 있다. | ||
+ | * [[java: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | ===== Dependencies ===== | ||
+ | < | ||
+ | testCompile group: ' | ||
+ | testCompile group: ' | ||
+ | </ | ||
+ | |||
+ | ===== Spock Feature Method 기본형태(test method) ===== | ||
+ | <code groovy> | ||
+ | // 일반적인 테스트 | ||
+ | |||
+ | def " | ||
+ | :given " | ||
+ | // 테스트용 데이터 초기화 | ||
+ | |||
+ | :when | ||
+ | // 테스트 대상 코드를 실행하고 실행 결과를 변수 등에 저장 | ||
+ | |||
+ | :then | ||
+ | // 테스트 결과에 대한 검증. | ||
+ | // 각 줄은 boolean 결과를 내는 Statement 로 작성한다. | ||
+ | // 만약 한 줄의 코드가 boolean statement가 아니고 복잡한 구문일 경우에는 | ||
+ | // 그 안의 assert boolean 구문에 ' | ||
+ | } | ||
+ | |||
+ | // Data Driven 테스트 | ||
+ | def " | ||
+ | expect: | ||
+ | // 각 데이터 변수로 테스트 수행 | ||
+ | |||
+ | where: "각 데이터 변수 설정" | ||
+ | a | b || c | ||
+ | 1 | 2 || 3 | ||
+ | 4 | 5 || 6 | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Mock Argument Capture ===== | ||
+ | [[http:// | ||
+ | |||
+ | <code groovy> | ||
+ | SaveCommentEvent capturedEvent | ||
+ | |||
+ | given: | ||
+ | ... | ||
+ | |||
+ | when: | ||
+ | .... | ||
+ | |||
+ | then: | ||
+ | 1 * eventBus.fireEvent({capturedEvent = it; true}) | ||
+ | |||
+ | capturedEvent instanceof SaveModelEvent | ||
+ | capturedEvent.newModel == newModel | ||
+ | capturedEvent.oldModel == oldModel | ||
+ | </ | ||
+ | |||
+ | * void 메도드의 경우 아래 참조. | ||
+ | |||
+ | ===== void method Mock - 인자 값 조정 혹은 throw exception ===== | ||
+ | * [[http:// | ||
+ | |||
+ | <code groovy> | ||
+ | // an interface with two methods: exists(user), | ||
+ | def userService = Mock(UserService) | ||
+ | |||
+ | // a controller to test, that will use mock of the service: | ||
+ | def controller = new UserController(userService) | ||
+ | |||
+ | def ' | ||
+ | given: | ||
+ | def username = ' | ||
+ | userService.exists(_ as User) >> false | ||
+ | userService.add(_ as User) >> { User user -> | ||
+ | throw new IllegalArgumentException(user.name) | ||
+ | } | ||
+ | |||
+ | when: | ||
+ | controller.addUser(username) | ||
+ | |||
+ | then: | ||
+ | def e = thrown(IllegalArgumentException) | ||
+ | e.message == username | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Mock 선언이 작동하지 않을 때 ===== | ||
+ | * 대상 Class 혹은 Method가 '' | ||
+ | |||
+ | ===== 기타 Mock ===== | ||
+ | <code groovy> | ||
+ | 0 * _ // 이 위 이후로는 어떠한 모의객체 호출 행위도 없어야 한다. | ||
+ | </ | ||
+ | |||
+ | ===== Spy ===== | ||
+ | * '' | ||
+ | * Spy객체 생성 후 Spring의 [[https:// | ||
+ | |||
+ | ===== 오류 / Error ===== | ||
+ | * [[https:// | ||
+ | * 1.0 버전에서 메소드가 여러개 override 돼 있을 경우 Type 지정이 명확하지 않으면 잘못된 메소드를 호출할 수도 있다.< | ||
+ | // method 가 여러개로 override 돼 있을 경우 어떤 것이 호출될지 알 수 없음. 특히 return 도 서로 다를 때. | ||
+ | expect: | ||
+ | SomeClass.method(null) == null | ||
+ | |||
+ | // 아래와 같이 파라미터와 리턴 타입을 모두 명시할 것. | ||
+ | when: | ||
+ | Result result = SomeClass.method(null as Request) | ||
+ | then: | ||
+ | result == null | ||
+ | </ | ||
+ | |||
+ | ===== Spock 1.3 & groovy 2.5 문제점 ===== | ||
+ | * groovy 2.4 로는 발생하지 않지만 2.5로 가면 '' | ||
+ | * '' | ||
+ | * spock 1.3 에서 Argument capture 방식 변경됨. [[https:// | ||
+ | * [[http:// | ||
+ | |||
===== 참조 ===== | ===== 참조 ===== | ||
* [[http:// | * [[http:// | ||
- | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[http:// |