사용자 도구

사이트 도구


java:spock

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
다음 판 양쪽 다음 판
java:spock [2017/04/06 17:41]
kwon37xi
java:spock [2020/06/26 12:28]
kwon37xi
줄 2: 줄 2:
   * [[https://github.com/spockframework/spock|Spock]]   * [[https://github.com/spockframework/spock|Spock]]
   * Test 코드가 설계 명세서 역할을 할 수 있을 정도로 가독성 높은 테스트를 짤 수 있다.   * Test 코드가 설계 명세서 역할을 할 수 있을 정도로 가독성 높은 테스트를 짤 수 있다.
 +  * [[java:spock:spock_reports|Spock Reports]] 테스트 결과를 문서화 해준다
   * [[http://docs.spockframework.org|Spock Framework Reference Documentation]] [[http://spockframework.github.io/spock/docs/1.0/index.html|1.0]]   * [[http://docs.spockframework.org|Spock Framework Reference Documentation]] [[http://spockframework.github.io/spock/docs/1.0/index.html|1.0]]
   * [[http://meetspock.appspot.com/|Spock Web Console]]   * [[http://meetspock.appspot.com/|Spock Web Console]]
   * [[https://leanpub.com/spockframeworknotebook|Spocklight notebook]]   * [[https://leanpub.com/spockframeworknotebook|Spocklight notebook]]
 +  * [[https://solidsoft.wordpress.com/2018/09/03/spock-1-2-hassle-free-spring-beans-mocking-in-integration-tests/|Spock 1.2 – hassle-free Spring beans mocking in integration tests | Solid Soft]]
  
 ===== Dependencies ===== ===== Dependencies =====
 <code> <code>
-testCompile group: 'org.spockframework', name: 'spock-core', version: '1.0-groovy-2.4'+testCompile group: 'org.spockframework', name: 'spock-core', version: '1.1-groovy-2.4'
 testCompile group: 'cglib', name: 'cglib-nodep', version: '3.2.4' // Class Mocking 할 때 필요. testCompile group: 'cglib', name: 'cglib-nodep', version: '3.2.4' // Class Mocking 할 때 필요.
 </code> </code>
줄 60: 줄 62:
 capturedEvent.newModel == newModel capturedEvent.newModel == newModel
 capturedEvent.oldModel == oldModel capturedEvent.oldModel == oldModel
 +</code>
 +
 +  * void 메도드의 경우 아래 참조.
 +
 +===== void method Mock - 인자 값 조정 혹은 throw exception =====
 +  * [[http://farenda.com/spock/spock-framework-mock-throw-exception/|Spock Framework Mock throw exception - Java Programming Tutorials]]
 +
 +<code groovy>
 +    // an interface with two methods: exists(user), add(user)
 +    def userService = Mock(UserService)
 + 
 +    // a controller to test, that will use mock of the service:
 +    def controller = new UserController(userService)
 + 
 +    def 'should throw exception for invalid username'() {
 +        given:
 +        def username = 'Joffrey Baratheon'
 +        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
 +    }
 </code> </code>
  
 ===== Mock 선언이 작동하지 않을 때 ===== ===== Mock 선언이 작동하지 않을 때 =====
   * 대상 Class 혹은 Method가 ''**final**''로 선언돼 있으면 아무 오류없이 Mock 이 작동하지 않는다.   * 대상 Class 혹은 Method가 ''**final**''로 선언돼 있으면 아무 오류없이 Mock 이 작동하지 않는다.
 +
 +===== 기타 Mock  =====
 +<code groovy>
 +0 * _                           // 이 위 이후로는 어떠한 모의객체 호출 행위도 없어야 한다.
 +</code>
 +
 +===== Spy =====
 +  * ''Spy()'' 사용시 스파이 대상 객체에 필드 인젝션이 안된다.
 +  * Spy객체 생성 후 Spring의 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/util/ReflectionTestUtils.html|ReflectionTestUtils]]를 이용해서 따로 주입해주면 된다.
  
 ===== 오류 / Error ===== ===== 오류 / Error =====
줄 78: 줄 118:
 result == null result == null
 </code> </code>
 +
 +===== Spock 1.x & groovy 2.5 문제점 =====
 +  * ''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 으로 무조건 간주하는 문제.
 +
  
 ===== 참조 ===== ===== 참조 =====
java/spock.txt · 마지막으로 수정됨: 2020/08/27 13:35 저자 kwon37xi