定义了PrepareForTest时的Powermock ClassNotPreparedException

时间:2019-05-16 13:22:52

标签: java android kotlin mockito powermock

在测试中,我想模拟一个没有构造函数的类,而该构造函数为此我找到了最终的类;

  

MessageAnimation

class MessageAnimation {

    fun fadeIn(layout: View, delay: Int) {
        val showMessage = AlphaAnimation(0.0f, 1.0f)
        showMessage.fillAfter = true
        showMessage.startOffset = delay.toLong()
        showMessage.duration = 1500
        layout.startAnimation(showMessage)
    }

    fun fadeOut(view: View, delay: Int) {
        val showMessage = AlphaAnimation(1.0f, 0.0f)
        showMessage.fillAfter = true
        showMessage.startOffset = delay.toLong()
        showMessage.duration = 1500
        showMessage.start()
        view.startAnimation(showMessage)
    }

    fun moveUp(layout: LinearLayout, delay: Int) {
        val objectAnimator = ObjectAnimator.ofFloat(layout, "translationY", (-125.0).toFloat())
        objectAnimator.startDelay = delay.toLong()
        objectAnimator.duration = 1500
        objectAnimator.start()
    }

    fun moveDown(layout: LinearLayout, delay: Int) {
        val objectAnimator = ObjectAnimator.ofFloat(layout, "translationY", (-125.0).toFloat())
        objectAnimator.startDelay = delay.toLong()
        objectAnimator.duration = 1500
        objectAnimator.start()
    }

    fun moveOutOfScreen(layout: LinearLayout, delay: Int) {
        val objectAnimator = ObjectAnimator.ofFloat(layout, "translationY", (-300.0).toFloat())
        objectAnimator.startDelay = delay.toLong()
        objectAnimator.duration = 1500
        objectAnimator.start()
    }
}

在测试中,我想模拟此类,因此发现可以为此使用PowerMock。我定义了该类应该使用它,并且还通过定义了此函数来准备测试以使用它。但是,在模拟之前不准备此类,我仍然遇到错误。

有人知道这是为什么吗?

  

ActivityTest.kt

@RunWith(PowerMockRunner::class)
@PrepareForTest(MessageAnimation::class)
class ActivityTest {

    private val messageAnimationMock = mockStatic(MessageAnimation::class.java)
    // other code
}

错误:

org.powermock.api.mockito.ClassNotPreparedException: 

[Ljava.lang.Object;@1a6d8329
The class robot.fsrt.robotguest.common.animation.MessageAnimation not prepared for test.


    at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:29)
    at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:37)
    at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:10)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:49)
    at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:40)
    at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:62)
    at robot.fsrt.robotguest.activities.ActivityTest.<init>(ActivityTest.kt:24)

0 个答案:

没有答案