사용자 도구

사이트 도구


java:spock

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:spock [2020/06/26 14:33]
kwon37xi [Spock 1.3 & groovy 2.5 문제점]
java:spock [2020/08/27 13:35] (현재)
kwon37xi [Mock Argument Capture]
줄 45: 줄 45:
  
 ===== Mock Argument Capture ===== ===== Mock Argument Capture =====
-[[http://stackoverflow.com/questions/22111212/how-to-do-argument-capture-with-spock-framework|How to do argument capture with spock framework? - Stack Overflow]] : 아이디어는 맞지만 실제 구현에 오류가 있다. 다음과 같이 수정. 
- 
 <code groovy> <code groovy>
-SaveCommentEvent capturedEvent +// Argument capture 
- +def extern = null 
-given: +  
- ... +1 * mock.foo( { extern = it; it.size() > 0 })  // 1.2 방식 
- +1 * mock.foo( { it.size() > 0 }) >> { extern it[0] } // 1.3 방식
-when: +
- .... +
- +
-then: +
-1 * eventBus.fireEvent({capturedEvent = it; true}) +
- +
-capturedEvent instanceof SaveModelEvent +
-capturedEvent.newModel == newModel +
-capturedEvent.oldModel == oldModel+
 </code> </code>
  
-  * void 메드의 경우 아래 참조.+  * void 메드의 경우 아래 참조.
  
 ===== void method Mock - 인자 값 조정 혹은 throw exception ===== ===== void method Mock - 인자 값 조정 혹은 throw exception =====
줄 98: 줄 87:
 ===== 기타 Mock  ===== ===== 기타 Mock  =====
 <code groovy> <code groovy>
-0 * _                           // 이 위 이후로는 어떠한 모의객체 호출 행위도 없어야 한다.+0 * _   // 이 위 이후로는 어떠한 모의객체 호출 행위도 없어야 한다.
 </code> </code>
  
줄 119: 줄 108:
 </code> </code>
  
-===== Spock 1.3 & groovy 2.5 문제점 ===== +===== Spock 1.3 & groovy 2.5 문제/변경점 ===== 
-  * groovy 2.4 로는 발생하지 않지만 2.5로 가면 ''where''의 변수명에 따라 문제가 발생함. 변수명 수정 필요. +  * [[http://spockframework.org/spock/docs/1.3/release_notes.html|1.3 Release Notes]]
-  * ''where'' 의 변수명이 전혀 다른 메소드에서 다른 타입으로 사용되면, 그 다른 타입으로 인식하는 문제[[https://github.com/spockframework/spock/issues/880|Parameter type is inferred wrong when local variable with same name is present in unrelated method · Issue #880 · spockframework/spock]] +
-  * ''where'' 의 변수명이 import 한 클래스명의 소문자 카멜케이스와 일치하면 해당 class type 으로 무조건 간주하는 문제. +
-  * spock 1.3 에서 Argument capture 방식 변경됨. [[https://github.com/spockframework/spock/issues/970|Argument capture and return value in 1.3-RC1 · Issue #970 · spockframework/spock]] +
-  * [[http://spockframework.org/spock/docs/1.3/release_notes.html|Release Notes]]+
  
 +==== where 의 변수명이 서로 다른 메소드에 동일하게 존재하는데 타입이 다를경우 ====
 +
 +  * groovy 2.4 로는 발생하지 않지만 2.5로 가면 ''where''의 변수명에 따라 문제가 발생함. 변수명 수정 필요. groovy 의 문제가 아니라 spock & groovy 2.5 조합일 때 발생하는 버그로 보임.
 +  * ''where'' 의 변수명이 전혀 다른 메소드에서 다른 타입으로 사용되면, 그 다른 타입으로 인식하는 문제. **type 이 다르면 변수 이름도 다르게 해야 한다.** [[https://github.com/spockframework/spock/issues/880|Parameter type is inferred wrong when local variable with same name is present in unrelated method · Issue #880 · spockframework/spock]]
 +
 +==== Argument 의 각 줄을 assert 하게 변경되면서 capture 방식도 바뀜 ====
 +  * 1.2 에서는 Argument 의 Closure 블록에서 조건을 체크하려면 전체를 하나의 true/false 를 반환하게 만들어야 했다.
 +  * 1.3 부터는 각 줄에 대해 ''then'' 처럼 각자 assert 를 수행하기 때문에 그럴 필요가 없어짐.
 +
 +<code groovy>
 +// 1.2 방식
 +1 * mock.foo({ it.size() > 1 &&  it[0].length == 2 })
 +
 +// 1.3 - 한줄 한줄의 boolean 반환 결과를 자동 체크함.
 +1 * mock.foo({ 
 +    it.size() > 1
 +    it[0].length == 2 })
 +</code>
 +
 +  * 그러나 그로인해서  Argument capture 방식 변경됨. [[https://github.com/spockframework/spock/issues/970|Argument capture and return value in 1.3-RC1 · Issue #970 · spockframework/spock]]
 +
 +<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 방식
 +</code>
  
 ===== 참조 ===== ===== 참조 =====
java/spock.1593149590.txt.gz · 마지막으로 수정됨: 2020/06/26 14:33 저자 kwon37xi