사용자 도구

사이트 도구


java:spock

차이

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

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
다음 판 양쪽 다음 판
java:spock [2016/09/20 19:57]
kwon37xi
java:spock [2018/09/06 10:25]
kwon37xi
줄 4: 줄 4:
   * [[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://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>
 +
 +===== Spock Feature Method 기본형태(test method) =====
 +<code groovy>
 +// 일반적인 테스트
 + 
 +def "Feature Method name"() {
 +    :given "생략가능"
 +    // 테스트용 데이터 초기화
 + 
 +    :when
 +    // 테스트 대상 코드를 실행하고 실행 결과를 변수 등에 저장
 + 
 +    :then
 +    // 테스트 결과에 대한 검증.
 +    // 각 줄은 boolean 결과를 내는 Statement 로 작성한다.
 +    // 만약 한 줄의 코드가 boolean statement가 아니고 복잡한 구문일 경우에는 
 +    // 그 안의 assert boolean 구문에 'assert somebooleanexpression' 형태로 assert를 붙여야한다.
 +}
 + 
 +// Data Driven 테스트
 +def "Feature method name #a - #b = #c"(변수 a, 변수 b, 변수 c) {
 +    expect:
 +    // 각 데이터 변수로 테스트 수행
 + 
 +    where: "각 데이터 변수 설정"
 +    a | b || c
 +    1 | 2 || 3
 +    4 | 5 || 6
 +}
 </code> </code>
  
줄 29: 줄 61:
 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>
 +
 +===== Mock 선언이 작동하지 않을 때 =====
 +  * 대상 Class 혹은 Method가 ''**final**''로 선언돼 있으면 아무 오류없이 Mock 이 작동하지 않는다.
 +
 +===== Spy =====
 +  * ''Spy()'' 사용시 스파이 대상 객체에 필드 인젝션이 안된다.
 +  * Spy객체 생성 후 Spring의 [[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/util/ReflectionTestUtils.html|ReflectionTestUtils]]를 이용해서 따로 주입해주면 된다.
 +
 +===== 오류 / Error =====
 +  * [[https://github.com/spockframework/spock/issues/491|Spock and Slf4j combination causes compilation error]] : Spock에서 ''@groovy.util.logging.Slf4j'' 사용시에 ''Error:Groovyc: The current scope already contains a variable of the name $spock_valueRecorder'' 오류가 발생하는 경우가 있다. Logger 직접 선언할 것.
 +  * 1.0 버전에서 메소드가 여러개 override 돼 있을 경우 Type 지정이 명확하지 않으면 잘못된 메소드를 호출할 수도 있다.<code groovy>
 +// method 가 여러개로 override 돼 있을 경우 어떤 것이 호출될지 알 수 없음. 특히 return 도 서로 다를 때.
 +expect:
 +SomeClass.method(null) == null
 +
 +// 아래와 같이 파라미터와 리턴 타입을 모두 명시할 것.
 +when:
 +Result result = SomeClass.method(null as Request)
 +then:
 +result == null
 </code> </code>
  
줄 39: 줄 120:
   * [[https://myshittycode.com/2014/08/08/spock-reading-test-data-from-csv-file/|Spock: Reading Test Data from CSV File | My Shitty Code]]   * [[https://myshittycode.com/2014/08/08/spock-reading-test-data-from-csv-file/|Spock: Reading Test Data from CSV File | My Shitty Code]]
   * [[https://www.youtube.com/watch?v=GHrfO1OqhLs|Interesting nooks and crannies of Spock you (may) have never seen before - YouTube]]   * [[https://www.youtube.com/watch?v=GHrfO1OqhLs|Interesting nooks and crannies of Spock you (may) have never seen before - YouTube]]
 +    * [[http://mrhaki.blogspot.kr/2016/09/spocklight-custom-default-responses-for.html|Spocklight: Custom Default Responses for Stubs - Messages from mrhaki]]
java/spock.txt · 마지막으로 수정됨: 2020/08/27 13:35 저자 kwon37xi