MissingMethodException when using Groovy and JUnit 5 Assertions

Viewed 202

Trying to use JUnit 5 class Assertions, specifically assertAll() with Groovy.

Basic Java proves the following syntax:

@Test
public void someTest() {
    assertAll('heading',
        () -> assertTrue(firstName),
        () -> assertTrue(lastName)
    );
}

Since I'm stuck to Groovy v2.4, which does not support lambdas, what I do looks like (tried several options, however all of those provide the same error):

@Test
void 'Some test'() {
    assertAll('heading',
        { -> assert firstName },
        { -> assert lastName }
    )
}

Error message:

groovy.lang.MissingMethodException:
No signature of method: static org.junit.jupiter.api.Assertions.assertAll() is applicable for argument 
types: (path.GeneralTests$_Some_test_closure10...)
values: [path.GeneralTests$_Some_test_losure10@1ef13eab, ...]

Looks like the problem is with assertAll() method itself, exception is quite obvious, however that's quite a challenge to get the possible solution, since I had to switch to Groovy from Python just recently.

Indeed, I am going to take some courses with Groovy in future, but you know, you always have to provide results for yesterday.

Would highly appreciate your suggestions!

1 Answers

In this particular case, you need to cast Groovy closure to org.junit.jupiter.api.function.Executable interface explicitly:

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.function.Executable

import static org.junit.jupiter.api.Assertions.*

class Test {
    @Test
    void 'Some test'() {
        assertAll('heading',
                { assertTrue(true) } as Executable,
                { assertFalse(false) } as Executable)
    }
}
Related