Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin and JUnit 5 assert an exception is thrown: separate declaration and execution with assertFailsWith

In Kotlin with JUnit5 we can use assertFailsWith

In Java with JUnit5 you can use assertThrows

In Java, if I want to separate the declaration of an executable from the execution itself, in order to clarify the tests in a Given-Then-When form, we can use JUnit5 assertThrows like this:

@Test
@DisplayName("display() with wrong argument command should fail" )
void displayWithWrongArgument() {

    // Given a wrong argument
    String arg = "FAKE_ID"

    // When we call display() with the wrong argument
    Executable exec = () -> sut.display(arg);

    // Then it should throw an IllegalArgumentException
    assertThrows(IllegalArgumentException.class, exec);
}

In Kotlin we can use assertFailsWith:

@Test
fun `display() with wrong argument command should fail`() {

    // Given a wrong argument
    val arg = "FAKE_ID"

    // When we call display() with the wrong argument
    // ***executable declaration should go here ***

    // Then it should throw an IllegalArgumentException
    assertFailsWith<CrudException> { sut.display(arg) }
}

But, how we can separate the declaration and the execution in Kotlin with assertFailsWith?

like image 344
p3quod Avatar asked Apr 27 '19 06:04

p3quod


People also ask

How do you assert exceptions in JUnit Kotlin?

Using Kotlin's assertFailsWith Method The Kotlin standard library also provides a function to test exceptions. We can use the assertFailsWith method to assert that a block of code fails with an exception type. In the example above, the assertion passes when the block throws an ArrayIndexOutOfBoundsException.

How do you throw an exception in JUnit 5?

In JUnit 5, to write the test code that is expected to throw an exception, we should use Assertions. assertThrows(). In the given test, the test code is expected to throw an exception of type ApplicationException or its subtype. Note that in JUnit 4, we needed to use @Test(expected = NullPointerException.

How do you assert an exception in JUnit?

When using JUnit 4, we can simply use the expected attribute of the @Test annotation to declare that we expect an exception to be thrown anywhere in the annotated test method. In this example, we've declared that we're expecting our test code to result in a NullPointerException.


2 Answers

Just declare a variable like you did in Java:

@Test
fun `display() with wrong argument command should fail`() {

    // Given a wrong argument
    val arg = "FAKE_ID"

    // When we call display() with the wrong argument
    val block: () -> Unit = { sut.display(arg) }

    // Then it should throw an IllegalArgumentException
    assertFailsWith<CrudException>(block = block)
}
like image 65
awesoon Avatar answered Oct 12 '22 15:10

awesoon


in my example you can do like this :

@Test
fun divide() {
    assertThrows(
        ArithmeticException::class.java,
        { MathUtils.divide(1, 0) },
        "divide by zero should trow"
    )
}
like image 23
Sana Ebadi Avatar answered Oct 12 '22 16:10

Sana Ebadi