问题描述
尝试使用JUnit 5类断言,特别是使用Groovy的assertAll()。
基本Java证明以下语法:
@Test
public void sometest() {
assertAll('heading',() -> assertTrue(firstName),() -> assertTrue(lastName)
);
}
由于我坚持使用不支持lambda的Groovy v2.4,所以我的工作看起来像样(尝试了几个选项,但是所有这些选项都提供相同的错误):
@Test
void 'Some test'() {
assertAll('heading',{ -> assert firstName },{ -> assert lastName }
)
}
错误消息:
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,...]
看起来问题出在assertAll()方法本身上,异常非常明显,但是要找到可能的解决方案是一个很大的挑战,因为我最近刚从Python切换到Groovy。
确实,将来我将与Groovy一起上一些课程,但是您知道,您始终必须提供昨天的成绩。
非常感谢您的建议!
解决方法
在这种情况下,您需要明确将Groovy闭包强制转换为org.junit.jupiter.api.function.Executable
接口:
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)
}
}