4

I have an Android Activity that implements an delegate interface which is called IMeetingRoomDelegate

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

I want to always assure that my Activity is always an implementation of IMeetingRoomDelegate.

How would I check this in Kotlin? I so far have this:

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

I've tried seeing what the issue is myself but the test runner is barely giving anything substantial away.

The error

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.
Marc Freeman
  • 713
  • 2
  • 7
  • 30

1 Answers1

3

You could use the reified generics instead of asserting the reflections:

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

private inline fun <reified T> T.implementsInterface(): Boolean {
    return this is IMeetingRoomDelegate
}
Animesh Sahu
  • 7,445
  • 2
  • 21
  • 49
  • It still throws the same error ``` 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.``` – Marc Freeman Apr 23 '20 at 08:24
  • @MarcFreeman could you confirm that activity implements the interface, cuz it works fine for me! – Animesh Sahu Apr 23 '20 at 08:26
  • I can 100% confirm that the activity implements the interface. I'll post it as another answer. – Marc Freeman Apr 23 '20 at 08:27
  • A simple example Any returns true with the hirearchy https://pl.kotl.in/SG06ZxRbX – Animesh Sahu Apr 23 '20 at 08:31
  • @MarcFreeman Could you post full exception in your question to help you further? – Animesh Sahu Apr 23 '20 at 08:37
  • Done, I've added the error and activity to the question – Marc Freeman Apr 23 '20 at 08:42
  • Also, apologies for editing your answer, I'm still half alseep – Marc Freeman Apr 23 '20 at 08:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212326/discussion-between-animesh-sahu-and-marc-freeman). – Animesh Sahu Apr 23 '20 at 08:50
  • For anyone reading, turns out I ran the code another time and it worked :/ accepting this as the correct answer. – Marc Freeman Apr 23 '20 at 08:53
  • 1
    TO CLARIFY THE ABOVE 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` – Animesh Sahu Apr 23 '20 at 08:55
  • 1
    I also reworked the answer to make it as generic as possible: inline fun T.implementsInterface(): Boolean { return this is U } usage: assertTrue(this.mainActivity.implementsInterface()) – Marc Freeman Apr 23 '20 at 09:09