验证伴随对象方法的单元测试(模拟伴随对象)

2022-09-04 07:10:36

切换到 Kotlin 时,静态方法被移动到伴随对象中。但是,没有明显的方法来单元测试其他调用这些“静态方法”的方法。

在Java中,我们可以使用PowerMockito的MockStatic(SomeClass.class)来验证在被测方法中调用的静态方法。PowerMock 在 Kotlin 中失去了魔力。

为了进行测试,我创建了以下类。

public class DummyJava {
    static public Void staticMechod(){
          System.out.print("Dummy method is called");
          return null;
     }
}

class DummyCompanion {
    companion object {
        fun someCompanionMethod(){
            System.out.printf("companion method is called\n")
        }
    }
}

现在有另一个类调用DummyCompanion.someCompanion

public class DummyWrapper {
    public void callAStaticMethod(){
        DummyJava.staticMechod();
    }

    public void callCompanionMethod(){
        DummyCompanion.Companion.someCompanionMethod();
    }
}

为了单元测试,我们使用了以下内容callAStaticMethod()

@RunWith(PowerMockRunner.class)
@PrepareForTest({DummyJava.class, DummyCompanion.Companion.class})
public class staticClassTest {
    //This case works
    @Test 
    public void testForStaticMethod() {
        PowerMockito.mockStatic(DummyJava.class);   
        DummyWrapper testObject = new DummyWrapper();

        Mockito.when(DummyJava.staticMechod()).thenCallRealMethod();

        testObject.callAStaticMethod();

        PowerMockito.verifyStatic(Dummy.class);
        DummyJava.staticMechod();
    }

    //This case doesn't work. It always passes.
    @Test
    public void testForCompanionMethod() {
        PowerMockito.mockStatic(DummyCompanion.Companion.class);
        DummyWrapper testObject = new DummyWrapper();
        testObject.callCompanionMethod();
PowerMockito.verifyStatic(DummyCompanion.Companion.class,Mockito.times(1));
        DummyCompanion.Companion.someCompanionMethod();
}

我的问题是如何验证伴随方法的调用。


答案 1

解决方案 1:在调用类中添加调用方函数

public class DummyWrapper {
val foo get() = DummyCompanion.Companion

public void callAStaticMethod(){
    foo.staticMechod();
}

public void callCompanionMethod(){
    foo.someCompanionMethod();
}
}

在测试类中,我们可以使用 Mockito 为函数提供存根,并验证它被调用。get()

@Test
fun testCase{
....
val mockCompanionObj: DummyCompanion.Companion = mock()
val wrapper = DummyWrapper()

whenever(wrapper.foo).thenReturn(mockCompanionObj)
wrapper.callCompanionMethod()
verify(mockCompanionObj).someCompanionMethod()
....
}

解决方案 2:使用 Mockk在 Mockk 中嘲笑伴随对象很容易。无需在源代码中插入测试接口对象。

 @Test
 fun testCompanionObject() {
    //Mock the companion object
    mockkObject(DummyCompanion.Companion)

    //define the stubbing bechavior of a companion object method
    every { DummyCompanion.Companion.companionMethod() } answers { stubMethod() }

    val testObject = DummyWrapper()

    //Call a method that calls the companion object method
    //You can verify stubMethod() is called
    testObject.callCompanionMethod()

    verify(exactly = 1) { DummyCompanion.someCompanionMethod() }
}

有关详细信息,请参阅莫克


答案 2

你也可以使用PowerMock做到这一点,它是这样的:

@RunWith(PowerMockRunner.class)
@PrepareForTest({DummyCompanion.class})
public class staticClassTest {

    @Test
    public void testForCompanionMethod() {
       PowerMockito.mockStatic(DummyCompanion.class);
       DummyCompanion.Companion companionMock = PowerMockito.mock(DummyCompanion.Companion.class);
       Whitebox.setInternalState(
        DummyCompanion.class, "Companion",
        companionMock
       );

       DummyWrapper testObject = new DummyWrapper();
       testObject.callCompanionMethod();
       Mockito.verify(companionMock,Mockito.times(1)).someCompanionMethod();
    }
}

Kotlin 为 Java(在本例中为 Kotlin 类)创建了其 Companion 子类的静态字段,该字段可以使用 PowerMock 的工具将其设置为模拟实例,以便以后验证方法调用。DummyCompanionCompanionWhiteBox.setInternalStateCompanion


推荐