Check all assertion and verifications when using mockk verification together with assertion libraries

1.3k Views Asked by At

I want the test to report all assertions and verifications. So both the mockk verification AND the the assertion library (in this case, KotlinTest) assertions should run and not shortcircuit.

In other words I don't want the test to stop ...

verify(exactly = 1) { mock.methodcall(any()) } // ... here
success shouldBe true // how can I check this line too

nor ...

success shouldBe true // ... here
verify(exactly = 1) { mock.methodcall(any()) }  // how can I check this line too

How to do this? I am open to use just one tool if I can do both with it.

1

There are 1 best solutions below

1
On BEST ANSWER

As per your comment, you said you are using KotlinTest.

In KotlinTest, I believe you can use assertSoftly for the behavior you want:

Normally, assertions like shouldBe throw an exception when they fail. But sometimes you want to perform multiple assertions in a test, and would like to see all of the assertions that failed. KotlinTest provides the assertSoftly function for this purpose.

assertSoftly {
  foo shouldBe bar
  foo should contain(baz)
}

If any assertions inside the block failed, the test will continue to run. All failures will be reported in a single exception at the end of the block.

And then, we can convert your test to use assertSoftly:

assertSoftly {
    success shouldBe true
    shouldNotThrowAny {
        verify(exactly = 1) { mock.methodcall(any()) }
    }
}

It's necessary to wrap verify in shouldNotThrowAny to make assertSoftly aware of it when it throws an exception