【问题标题】:JUnit assert that an Activity is implementation of an interfaceJUnit 断言 Activity 是接口的实现
【发布时间】:2020-08-06 11:52:35
【问题描述】:

我有一个 Android Activity,它实现了一个名为 IMeetingRoomDelegate 的委托接口

interface IMeetingRoomDelegate {
    fun onMeetingRoomFragmentClicked(homeFragment: MeetingRoomHomeFragment, meetingRoom: ParcelableMeetingRoomData)
}

我想始终确保我的 Activity 始终是 IMeetingRoomDelegate 的实现。

如何在 Kotlin 中检查这一点?我到目前为止有这个:

@RunWith(AndroidJUnit4::class)
class GivenTheMainActivityIsLoaded {

    @get:Rule
    val activityRule =  ActivityTestRule<MainActivity>(MainActivity::class.java)
    private lateinit var mainActivity: MainActivity

    @Before
    fun setup() {
        this.mainActivity = activityRule.activity
    }

    @Test
    fun thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent() {
        assertTrue(implementsInterface(this.mainActivity::class.java))
    }

    private fun implementsInterface(interf: Class<*>): Boolean {
        return interf is IMeetingRoomDelegate
    }
}

我已经尝试自己查看问题所在,但测试运行者几乎没有给出任何实质性的东西。

错误

java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at activities.GivenTheMainActivityIsLoaded.thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent(MainActivitySpec.kt:54)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)

Tests ran to completion.

【问题讨论】:

    标签: android kotlin junit interface delegates


    【解决方案1】:

    您可以使用具体化的泛型而不是断言反射:

    @Test
    fun thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent() {
        assertTrue(this.mainActivity.implementsInterface())
    }
    
    private inline fun <reified T> T.implementsInterface(): Boolean {
        return this is IMeetingRoomDelegate
    }
    

    【讨论】:

    • 它仍然在 org.junit.Assert.fail(Assert.java:86) 在 org.junit.Assert.assertTrue(Assert.java: 41) at org.junit.Assert.assertTrue(Assert.java:52) at activity.GivenTheMainActivityIsLoaded.thenThereShouldAlsoBeAnInstanceOfIMeetingRoomDelegatePresent(MainActivitySpec.kt:54) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners .model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.```
    • @MarcFreeman 你能确认活动实现了接口吗,因为它对我来说很好用!
    • 我可以 100% 确认活动实现了接口。我会把它作为另一个答案发布。
    • 澄清以上 STMT:I edited the answer within 1 minute of posting, OP maynot be updated his browser, I just put String in the implementsInterface() as i was testing with my ide
    • 我还修改了答案以使其尽可能通用:inline fun T.implementsInterface(): Boolean { return this is U } 用法:assertTrue(this.mainActivity .implementsInterface())
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多