2

Groovy は初めてで、アプリケーションに Spock フレームワークを実装しようとしています。ここに私のテストコードがあります:

def "Test class with mock object"()  {

        setup:
        SomeObject sp = Mock()
        test= TestClass()

        when:
        System.out.println('comes here');
        push.exec(sp)

        then:
        sp.length == 1

    }

TestClassこれは、テストメソッドでキャッチするか、再度スローする必要がある例外をスローしています。私は試した

try {

  push.exec(sp)
} catch (Exception e) {

}

しかし、まだ得ています

groovy.lang.MissingMethodException: No signature of method: test.spock.TestClassTest.TestClass() is applicable for argument types: () values: []
Possible solutions: use([Ljava.lang.Object;), use(java.util.List, groovy.lang.Closure), use(java.lang.Class, groovy.lang.Closure), dump(), with(groovy.lang.Closure), each(groovy.lang.Closure)
4

2 に答える 2

5

これが Spock で例外を処理する正しい方法です。

def "Test class with mock object"()  {

    setup:
    SomeObject sp = Mock()
    test= TestClass()

    when:
    System.out.println('comes here');
    push.exec(sp)

    then:
    thrown(YourExceptionClass)
    sp.length == 1

}

または、例外でいくつかのデータをチェックしたい場合は、次のようなものを使用できます。

    then:
    YourExceptionClass e = thrown()
    e.cause == null
于 2013-04-30T10:49:17.697 に答える
5

ではなくtest = TestClass()、 である必要がありますtest = new TestClass()。予想される例外をテストするSpecification.thrownには、try-catch の代わりに使用します。例については、 Spock のJavadocを参照してください。

于 2013-04-09T18:53:58.633 に答える