0

Kotlin クラスと Groovy/Spock テストがあります。Kotlin クラスをモックし、モックにプロパティ値を設定すると、Kotlin に引き継がれません。

Kotlin エンティティ クラスとそれを使用するクラスを次に示します。

open class TestEntity(var prop: String) {

}

class AClassUnderTest {
    fun useATestEntity(testEntity: TestEntity) {
        System.out.println("Checking it isn't null within Kotlin code: " + (testEntity.prop))
        System.out.println("Code which assumes it is not null: " + testEntity.prop.length)
    }
}

そして、TestEntity をモックし、getProp() メソッドをモックし、Kotlin メソッドを呼び出してそれを使用するテストを次に示します。

class AClassUnderTestTest extends Specification {
    def "UseATestEntity"() {
        given:
        def cut = new AClassUnderTest()
        def testEntityMock = GroovyMock(TestEntity)
        testEntityMock.getProp() >> "abc"
        System.out.println("Checking it isn't null after mocking: " + (testEntityMock.prop))

        when:
        System.out.println("Checking it isn't null during when clause: " + (testEntityMock.prop))
        cut.useATestEntity(testEntityMock)

        then:
        noExceptionThrown()
    }
}

予想される動作は、デモ println のすべてが「abc」を示し、メソッドが成功することです。

観測された動作は、Kotlin 内の println がプロパティが null であることを示し、メソッドが失敗することです。

Checking it isn't null after mocking: abc
Checking it isn't null during when clause: abc
Checking it isn't null within Kotlin code: null

Expected no exception to be thrown, but got 'java.lang.NullPointerException'

私は何を間違っていますか?

Kotlin クラスをモックして値を設定し、モックされた値が Kotlin コードと Groovy コードで取得されるようにするにはどうすればよいですか?

すでに試しました:

  • GroovyMock() の代わりに GroovyStub() を使用 - 違いなし
  • GroovyMock() の代わりに Mock()/Stub() を使用します。その場合、すべての println の偶数 Groovy でプロパティが null になります (!!?)
  • 「testEntityMock.getProp() >>」の代わりに「testEntityMock.prop >>」をモック - 違いなし
4

1 に答える 1