lundi 28 août 2017

How do I test generators for delegated properties?

In my current project there is a class that will later on be implemented by many others. This class provides some generators for delegated properties.

abstract class BaseClass {
    protected val delegated1 get() = new Delegated1Impl()
    protected val delegated2 get() = new Delegated2Impl()
    ...
}

This base class can be used this way:

class Example : BaseClass() {
    var field1 by delegated1
    var field2 by delegated2
}

Now I want to test these delegated generators. Some of them contain logic which I want to test, but for now I only want to know that everytime they are called they return a new instance.

Now my question is: how can I test these generators?
The generators are not visible outside of extending classes so I cannot simply create an instance of it and call these methods.

@Test
fun `delegated1 should always return a new instance`() {
    val target = object: BaseClass()

    val first = target.delegated1    // This does not work since it is protected
    val second = target.delegated1

    assertTrue(first !== second)
}

Aucun commentaire:

Enregistrer un commentaire