从阴影方法调用真实对象的方法将导致无限递归

时间:2019-06-13 01:10:30

标签: android unit-testing mocking android-testing robolectric

我创建了一个影子类,该类调用http://robolectric.org/extending中所述的真实对象:

@Implements(View::class)
class MyShadowView {

    @RealObject
    private lateinit var realView: View

    @Implementation
    fun animate(): ViewPropertyAnimator {
        return realView.animate() // this call ends up calling my shadow's animate() function recursively
    }
}

但是,当我的shadow方法被执行时,它会导致无限递归。

我在做什么错了?

(我正在使用Robolectric 4.2。)

1 个答案:

答案 0 :(得分:1)

根据http://robolectric.org/javadoc/4.1/org/robolectric/shadow/api/Shadow.html,应该是这样的:

    @Implementation
    fun animate(): ViewPropertyAnimator {
        Shadow.directlyOn(realView, View::class.java).animate()
    }