Suppose I have code that assigns a method to a Runnable
, for example:
class C1{
Runnable r;
}
class C2{
void f(){}
}
var c1 = new C1();
var c2 = new C2();
c1.r = c2::f;
I want to write a unit test to assert that r
is indeed assigned f
, perhaps like this:
assertThat(c1.r).isEqualTo(c2::f); // Error: object is not a functional interface
Casting to Runnable
does not help:
expected: ...$$Lambda$302/0x0000000800cd3518@1fe20588
but was : ...$$Lambda$301/0x0000000800c9fac0@77167fb7
In C# I can assign a method to an Action
variable this assertion works. What's the equivalent in Java?
I am open to switching from Runnable
to some other type, if that helps.
To give some more context: I'm trying to implement event-based decopuling like Arlo Belshee describes here: https://arlobelshee.com/decoupled-design/. Here's my C# implementation for comparison: https://jay.bazuzi.com/Decoupled-Design/