在Kotlin中为测试类创建通用的func

时间:2019-01-28 18:03:41

标签: generics kotlin

我正在尝试优化A的测试类。这是代码结构。具有三个捕获功能,我正在尝试一个

Internal class A {
     inner class X1: L1 {
    }

     inner class X2: L2 {
    }

     inner class X3: L3 {
    }
}
Interface L1 { }
Interface L2 { }
Interface L3 { }

Interface obj {
 addListener(L1)
addListener(L2)
addListener(L3)
}

测试类

Class Atest { 
    private fun capturetL1(): Capture<A.X1> {
            val events: Capture<A.X1> = EasyMock.newCapture()
            EasyMock.expect(obj.addListener(EasyMock.capture(events)))
            EasyMock.expectLastCall<Any>().once()
            return events
        }
    private fun capturetL2(): Capture<A.X2> {
            val events: Capture<A.X2> = EasyMock.newCapture()
            EasyMock.expect(obj.addListener(EasyMock.capture(events)))
            EasyMock.expectLastCall<Any>().once()
            return events
        }

    private fun capturetL3(): Capture<A.X3> {
            val events: Capture<A.X3> = EasyMock.newCapture()
            EasyMock.expect(obj.addListener(EasyMock.capture(events)))
            EasyMock.expectLastCall<Any>().once()
            return events
        }


}

我正在尝试编写一种通用的捕获方法。但是遇到类型转换问题。

fun <E : A> capture(event: Capture<E>) {
val events = EasyMock.newCapture()
EasyMock.expect(obj.addListener(EasyMock.capture(events))) 
EasyMock.expectLastCall<Any>().once()
return events
}

0 个答案:

没有答案