문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
java:spock [2017/07/10 20:43] kwon37xi [Dependencies] |
java:spock [2020/08/27 13:35] kwon37xi [Mock Argument Capture] |
||
---|---|---|---|
줄 2: | 줄 2: | ||
* [[https:// | * [[https:// | ||
* Test 코드가 설계 명세서 역할을 할 수 있을 정도로 가독성 높은 테스트를 짤 수 있다. | * Test 코드가 설계 명세서 역할을 할 수 있을 정도로 가독성 높은 테스트를 짤 수 있다. | ||
+ | * [[java: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
===== Dependencies ===== | ===== Dependencies ===== | ||
줄 43: | 줄 45: | ||
===== Mock Argument Capture ===== | ===== Mock Argument Capture ===== | ||
- | [[http:// | ||
- | |||
<code groovy> | <code groovy> | ||
- | SaveCommentEvent capturedEvent | + | // Argument capture |
+ | def extern = null | ||
+ | |||
+ | 1 * mock.foo( { extern = it; it.size() > 0 }) // 1.2 방식 | ||
+ | 1 * mock.foo( { it.size() > 0 }) >> { extern = it[0] } // 1.3 방식 | ||
+ | </ | ||
- | given: | + | * void 메소드의 경우 아래 참조. |
- | ... | + | |
- | when: | + | ===== void method Mock - 인자 값 조정 혹은 throw exception ===== |
- | .... | + | * [[http:// |
- | + | ||
- | then: | + | |
- | 1 * eventBus.fireEvent({capturedEvent = it; true}) | + | |
- | capturedEvent instanceof SaveModelEvent | + | <code groovy> |
- | capturedEvent.newModel | + | // an interface with two methods: exists(user), |
- | capturedEvent.oldModel | + | def userService |
+ | |||
+ | // a controller to test, that will use mock of the service: | ||
+ | def controller | ||
+ | |||
+ | 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 | ||
+ | } | ||
</ | </ | ||
===== Mock 선언이 작동하지 않을 때 ===== | ===== Mock 선언이 작동하지 않을 때 ===== | ||
* 대상 Class 혹은 Method가 '' | * 대상 Class 혹은 Method가 '' | ||
+ | |||
+ | ===== 기타 Mock ===== | ||
+ | <code groovy> | ||
+ | 0 * _ // 이 위 이후로는 어떠한 모의객체 호출 행위도 없어야 한다. | ||
+ | </ | ||
===== Spy ===== | ===== Spy ===== | ||
줄 81: | 줄 106: | ||
then: | then: | ||
result == null | result == null | ||
+ | </ | ||
+ | |||
+ | ===== Spock 1.3 & groovy 2.5 문제/ | ||
+ | * [[http:// | ||
+ | |||
+ | ==== where 의 변수명이 서로 다른 메소드에 동일하게 존재하는데 타입이 다를경우 ==== | ||
+ | |||
+ | * groovy 2.4 로는 발생하지 않지만 2.5로 가면 '' | ||
+ | * '' | ||
+ | |||
+ | ==== Argument 의 각 줄을 assert 하게 변경되면서 capture 방식도 바뀜 ==== | ||
+ | * 1.2 에서는 Argument 의 Closure 블록에서 조건을 체크하려면 전체를 하나의 true/false 를 반환하게 만들어야 했다. | ||
+ | * 1.3 부터는 각 줄에 대해 '' | ||
+ | |||
+ | <code groovy> | ||
+ | // 1.2 방식 | ||
+ | 1 * mock.foo({ it.size() > 1 && | ||
+ | |||
+ | // 1.3 - 한줄 한줄의 boolean 반환 결과를 자동 체크함. | ||
+ | 1 * mock.foo({ | ||
+ | it.size() > 1 | ||
+ | it[0].length == 2 }) | ||
+ | </ | ||
+ | |||
+ | * 그러나 그로인해서 | ||
+ | |||
+ | <code groovy> | ||
+ | // Argument capture | ||
+ | def extern = null | ||
+ | |||
+ | 1 * mock.foo( { extern = it; it.size() > 0 }) // 1.2 방식 | ||
+ | 1 * mock.foo( { it.size() > 0 }) >> { extern = it[0] } // 1.3 방식 | ||
</ | </ | ||