Getting access to class/interface properties from a companion object in Kotlin

57 Views Asked by At

I'm trying to make tests more flexible with providing interface with an abstract property for implementing in different classes.

The thing is that we can't get access to the property outside the companion object

Maybe any ideas how can I implement it another way?

I was trying some options like this :

interface InterfaceTest {
    val property: Property

    companion object {
        val necessaryData = getInformation(property.data)
    }

    @Test
    fun `dummy test`(){
    ...
    }
}

class FirstClassTest : InterfaceTest {
    override val property = Property(data = "true data")
}

class SecondClassTest : InterfaceTest {
    override val property = Property(data = "another data")
}

It can be an abstract class too:

abstract class AbstractTest {
    abstract val property: Property

    companion object {
        val necessaryData = getInformation(property.data)
    }

    @Test
    fun `dummy test`(){
    ...
    }
}

class FirstClassTest : AbstractTest() {
    override val property = Property(data = "true data")
}

class SecondClassTest : AbstractTest() {
    override val property = Property(data = "another data")
}
0

There are 0 best solutions below