Skip to content

Robo electric extension not inherited through nested classes when using abstract class #134

@dimpdash

Description

@dimpdash

Setup

I have a Domain test which is agnostic to robo electric and is actually in a separate gradle module with no dependencies

abstract class DomainTest {
        @Nested
         inner class CaseSpecificTest {
                 @Test
                  fun test() {}
         }
}

I then have another gradle module for android that implements the domain

@ExtendWith(RoboElectricTest)
AndroidSpecficImpDomainTest : DomainTest {}

Cannot put RoboElectricTest annotation on Domain test without breaking decoupling

Issue

This code is failing on the nested tests

JUnit5RobolectricTestRunnerHelper
...
            require(testClass.isExtendedWithRobolectric()) {
                "Test class ${testClass.name} is not extended with ${RobolectricExtension::class.simpleName}"
            }

I think its because isExtendedWithRobolectric only checks for the declaringClass but does not also check for the super class

internal fun Class<*>.isExtendedWithRobolectric(): Boolean {
    val isExtended = findAnnotations(ExtendWith::class.java).any { annotation ->
        val valueMethod = annotation.javaClass.getDeclaredMethod("value")

        @Suppress("UNCHECKED_CAST")
        val value: Array<Class<*>> = valueMethod.invoke(annotation) as Array<Class<*>>
        value.any { it.name == RobolectricExtension::class.java.name }
    }
    return if (isExtended) {
        true
    } else if (declaringClass != null) {
        declaringClass.isExtendedWithRobolectric()

#### Maybe could add some thing like this ####
    } else if (superClass != null) { 
        superClass.isExtendedWithRoboelectric()
     } else {
        false
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions