称为验证伴随对象方法的单元测试(模拟伴随对象) [英] Unit Testing verifying a companion object method is called (mocking a companion object)

查看:64
本文介绍了称为验证伴随对象方法的单元测试(模拟伴随对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到Kotlin时,静态方法将移入一个伴随对象.但是,没有明显的方法可以对称为静态方法"的其他方法进行单元测试.

When switching to Kotlin, static methods are moved into a companion object. However, there is no obvious way to unit test other methods which call these "static method".

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

In Java, we could use PowerMockito's MockStatic(SomeClass.class) to verify a static method is called in the method under test. PowerMock loses its magic in Kotlin.

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

For testing, I have create the following classes.

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()我们使用了以下

To unit test callAStaticMethod() We used the following

@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();
}

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

My question is how to verify the companion method is called.

推荐答案

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

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

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

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

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

In the test class, we can use Mockito to provide a stub for the get() function and verified it is called.

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

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

解决方案2:使用Mockk 在Mockk中模拟伴侣对象很容易.无需在源代码中插入测试接口对象.

Solution 2: using Mockk Mocking companion object in Mockk is easy. There is no need to insert a test interfacing object in the source code.

 @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() }
}

有关详细信息,请参见 Mockk

For details see Mockk

这篇关于称为验证伴随对象方法的单元测试(模拟伴随对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆