Hey folks, new to Kotlin in general and new to kot...
# announcements
j
Hey folks, new to Kotlin in general and new to kotlintest. My test class is extending a base class which extends BehaviorSpec. I’d like to execute a method on that base class from within a given block. I’ve tried things like
super.method()
, but I keep getting unresolved reference. Any tips are appreciated
s
can you share how, exactly, you’re trying to call the super method?
super.method()
should work fine normally
j
Sure
Copy code
class BindingSpec: AbstractRabbitMQTest({

    val specName = javaClass.simpleName

    given("A basic producer and consumer") {
This is defined inside
AbstractRabbitMQTest
Copy code
fun startContext(): ApplicationContext {
...
    }
right inside that given block I’d like to call that method
abstract class AbstractRabbitMQTest(body: AbstractBehaviorSpec.() -> Unit = {}): BehaviorSpec(body) {
s
This is an advanced hierarchy you’ve got going here lol
I’m not entirely sure if this is the right answer, but a
Foo.() -> Unit
-typed lambda isn’t actually anything special - it’s essentially a
(Foo) -> Unit
but with
this
in play
Which means that the lambda doesn’t have access to private members or the class’s
super
instance
j
What do you suggest I try?
s
Hmmm, it’s hard to say because I can’t test with your specific setup
let me pull up a scratch file real quick
j
I appreciate it
s
Agh, I wish I had a better answer for you unfortunately. I would maybe ask if you strictly need to have this implementing class extend your abstract class
j
Unfortunately yes because it contains static state that is shared across all the tests
all the tests that extend from it anyway
s
If you need to call the super constructor with a lambda that calls a method that internally calls a super method, you’ve got a bit of an ouroboros on your hands
j
hahaha
I’m surprised this is difficult
s
It might not be, I might just be dumb and not seeing a simple solution here haha
is there maybe a less crazy way to access the state you need? Maybe some top-level vals that are internal/package accessible? Is the parent class abstract? if not, maybe instead have a property that stores a reference to a singleton instance of said parent class or something?
j
unfortunately I need to create and teardown the object inside the given block
since it is only scoped for that test
s
ah I think I get what you mean
j
I’ve gotta run for now, but I appreciate the attempt!
s
Sorry I couldn’t be of more help
I gotta get some shuteye though haha
p
I have simplified your code this way:
Copy code
abstract class Parent(
    body: () -> Unit
) {
    fun needToCall() { }
}

class Child : Parent {
    constructor() : super({
        super.needToCall()
    })
}
As you can see error message is
Cannot access '<this>' before superclass constructor has been called
. So it is not possible to do what you want.
Why would you want to call a method on an object that is not initialized yet anyway? Can you make your method static, i.e. put it into companion object?
j
@Pavlo Liapota I’m not attempting to call the method in the child constructor, but yes I probably could move it to the companion. Thanks!