문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 다음 판 양쪽 다음 판 | ||
java:spock [2016/09/27 14:14] kwon37xi |
java:spock [2020/08/27 13:33] kwon37xi [Argument 의 각 줄을 assert 하게 변경되면서 capture 방식도 바뀜] |
||
---|---|---|---|
줄 2: | 줄 2: | ||
* [[https:// | * [[https:// | ||
* Test 코드가 설계 명세서 역할을 할 수 있을 정도로 가독성 높은 테스트를 짤 수 있다. | * Test 코드가 설계 명세서 역할을 할 수 있을 정도로 가독성 높은 테스트를 짤 수 있다. | ||
+ | * [[java: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
===== Dependencies ===== | ===== Dependencies ===== | ||
< | < | ||
- | testCompile group: ' | + | testCompile group: ' |
testCompile group: ' | testCompile group: ' | ||
</ | </ | ||
줄 41: | 줄 43: | ||
} | } | ||
</ | </ | ||
+ | |||
===== Mock Argument Capture ===== | ===== Mock Argument Capture ===== | ||
[[http:// | [[http:// | ||
줄 61: | 줄 64: | ||
</ | </ | ||
+ | * 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 문제/ | ||
+ | ==== 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 }) | ||
+ | </ | ||
+ | |||
+ | * 그러나 그로인해서 | ||
+ | * [[http:// | ||
+ | |||
+ | <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 방식 | ||
+ | </ | ||
===== 참조 ===== | ===== 참조 ===== | ||
* [[http:// | * [[http:// |