2

I'm in the process of converting my JUnit tests to AssertJ, but I'm running into some issues with AssertJ.

I'm not quite sure how to test a suspend function using AssertJ.

In JUnit, I can do this.

runTest {
    assertThrows<Exception> {
        //suspend function
        messagePublisher.publish(//message)
    }
}

However, in assertJ:

runTest {
    assertThatExceptionOfType(Exception::class.java).isThrownBy {
        // the same suspend function
        messagePublisher.publish(message)
    }
}

I get the error:

Suspension functions can be called only within coroutine body`

Is there a way to easily test suspension functions using assertJ? How can I use a coroutine body here?

bensalerno
  • 467
  • 1
  • 6
  • 22

0 Answers0