3

I'm trying to mock some interfaces using Mockk.

At some point, I have to create a mock that implements 2 interfaces.

For instance, using Moq in C#, I can do that :

// implementing multiple interfaces in mock
var foo = new Mock<IFoo>();
var disposableFoo = foo.As<IDisposable>();
// now IFoo mock also implements IDisposable :)
disposableFoo.Setup(df => df.Dispose());

Is there a way to do that with Mockk ?

If I try something like

val foo : IFoo = mockk()
every { (foo as AutoCloseable).close() } just Runs

I get the following error (which makes sense) :

com.example.IFoo$Subclass0 cannot be cast to java.lang.AutoCloseable

I saw in the documentation that I can specify additional interfaces with spyk, but I don't want to use an actual implementation here.

Is there a way to do that ? Is there another approach ?

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85

1 Answers1

9

It is possible with moreInterfaces parameter.

Example from documentation with spyk. Should work with mockk as well:

val spy = spyk(System.out, moreInterfaces = *arrayOf(Runnable::class))
oleksiyp
  • 2,659
  • 19
  • 15