PowerMockito Spy:使用CannotStubVoidMethodWithReturnValue调用实际方法结果

时间:2018-05-09 03:09:02

标签: android unit-testing kotlin mocking powermockito

我开始撞到墙上,但我完全不懂如何在间谍课上称呼真正的方法。

长话短说,我在我的测试类上监视一个私有方法。然后我想调用测试类的真正无效方法,但它说

  

' testedMethod'是一个 void方法,它不能返回值进行存根!

问题是我不需要它被打断。我也试过doCallRealMethod(),没用。

我使用Kotlin和Android Studio为我的Android项目运行一些单元测试。我有以下要测试的课程:

class MyClass {
   fun persistDeviceData(deviceInfo: DeviceInfo, saveCallback: SaveCallback) {
       val deviceObject = getDeviceObject()
          // some setters from deviceInfo here
       deviceObject.saveInBackground(callback)
   }

   // This method is created for unit testing purposes
   private fun getDeviceObject(): MyDeviceObject{
       return MyDeviceObject("someStringArgument")
   }
}

这是测试类

import io.kotlintest.KTestJUnitRunner
import io.kotlintest.specs.BehaviorSpec
import junit.framework.Assert
import org.junit.runner.RunWith
import org.mockito.Mockito

import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.powermock.modules.junit4.PowerMockRunnerDelegate

@RunWith(PowerMockRunner::class)
@PowerMockRunnerDelegate(KTestJUnitRunner::class)
@PrepareForTest(MyClass::class)
class MyClassTest: BehaviorSpec(){

    init {

        val deviceObject = PowerMockito.mock(MyDeviceObject::class.java)
        val myClassTest = PowerMockito.spy(MyClass())
        // Mocking private method call
        PowerMockito.doReturn(deviceObject).`when`(testedRepo,
                PowerMockito.method(
                        MyClass::class.java,
                        "getDeviceObject"))
        val a = SaveCallback { // Callback handling here }
        // Calling REAL method of spy class. Here it fails
        myClassTest.persistDeviceData(DeviceInfo(), a)
        Assert.assertTrue(true)
    }

}

在下一行调用实际方法时测试失败

  

myClassTest.persistDeviceData(DeviceInfo(),a)

这里是错误堆栈跟踪

  

org.mockito.exceptions.misusing.CannotStubVoidMethodWithReturnValue:   ' persistDeviceData'是一个 void方法,它不能返回值存根!   空洞通常与Throwables有关:       doThrow(例外)。当(模拟).someVoidMethod();   如果您需要将void方法设置为什么都不做,您可以使用:       doNothing()时(模拟).someVoidMethod();   有关更多信息,请查看Mockito.doNothing()的javadoc。

           

如果你不确定为什么你会在上面读错误。   由于上述语法的性质,可能会出现问题,原因是:   1.您尝试存根的方法是重载。确保您正在调用正确的重载版本。   2.在测试的某个地方,您正在查找最终方法。对不起,Mockito没有验证/存根最终方法。   3.使用when(spy.foo())。then()语法时,间谍是存根的。存根间谍更安全 -       - 使用doReturn | Throw()系列方法。更多关于Mockito.spy()方法的javadocs。   4.不支持在非公共父类上声明的模拟方法。

at MyClassTest.<init>(MyClass.kt:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at io.kotlintest.KTestJUnitRunner.<init>(KTestJUnitRunner.kt:9)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.powermock.reflect.internal.WhiteboxImpl.createInstance(WhiteboxImpl.java:1414)
at org.powermock.reflect.internal.WhiteboxImpl.invokeConstructor(WhiteboxImpl.java:1262)
at org.powermock.reflect.Whitebox.invokeConstructor(Whitebox.java:497)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$1.call(DelegatingPowerMockRunner.java:101)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$1.call(DelegatingPowerMockRunner.java:97)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.withContextClassLoader(DelegatingPowerMockRunner.java:132)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.createDelegate(DelegatingPowerMockRunner.java:96)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.<init>(DelegatingPowerMockRunner.java:64)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:165)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:47)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:107)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:69)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:36)
at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

1 个答案:

答案 0 :(得分:1)

回答我自己的问题 - 我错误地使用了PowerMockito的.when()方法。这是有效的代码:

@RunWith(PowerMockRunner::class)
@PrepareForTest(MyClass::class, DeviceObject::class)
class DeviceRepositoryImplTest{

    private val testedRepo = PowerMockito.spy(MyClass())
    private val deviceObject = PowerMockito.mock(DeviceObject())

    @Test
    fun persistDeviceData_ok(){
        // Mocking private method
        PowerMockito.`when`<ParseObject>(testedRepo,
            PowerMockito.method(
                        MyClass::class.java,
                            "getDeviceObject")).withNoArguments().thenReturn(deviceObject)

        // Creating callback for tested method
        val callback = SaveCallback {
            ...
        }

        ...

        testedRepo.persistDeviceData(DeviceInfo(), callback)

    }