我正在使用Vert.x 3.5.3
。我有这个(简单的)Verticle:
class DialPadVerticle : AbstractVerticle() {
private companion object : KLogging()
override fun start(future: Future<Void>) {
vertx.eventBus().consumer(Address.DIALPAD_COMBINATIONS) { message: Message<Int> ->
val input = message.body()
logger.info { "Received: $input" }
message.reply(JsonObject().put("result", DialPad().combinations(input)))
}
future.complete()
}
override fun stop(future: Future<Void>) {
logger.debug { "Stopping ${this.javaClass.simpleName} (${deploymentID()})...DONE" }
future.complete()
}
}
...并且我正在尝试使用实验性的vertx-junit5
为此构建一个单元测试框架。到目前为止,我有这个:
@ExtendWith(VertxExtension::class)
@DisplayName("Dial pad verticle should...")
internal class DialPadVerticleTest {
@BeforeEach
fun prepare(vertx: Vertx, testContext: VertxTestContext) {
vertx.deployVerticle(DialPadVerticle(), testContext.succeeding())
}
@Test
@Throws(Exception::class)
@DisplayName("Consume Message<Int> correctly")
fun `consume message correctly`(vertx: Vertx, testContext: VertxTestContext) {
vertx.eventBus().send<JsonObject>(Address.DIALPAD_COMBINATIONS, 5) {
// Assertions.assertThat(it.succeeded()).isTrue()
testContext.verify {
testContext.completeNow()
}
}
}
}
但是每次我尝试从IntelliJ中运行它时,我都会得到:
java.util.concurrent.TimeoutException: The test execution timed out
at io.vertx.junit5.VertxExtension.joinActiveTestContexts(VertxExtension.java:213)
at io.vertx.junit5.VertxExtension.beforeTestExecution(VertxExtension.java:171)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeTestExecutionCallbacks$4(TestMethodTestDescriptor.java:141)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:155)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeTestExecutionCallbacks(TestMethodTestDescriptor.java:140)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:111)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:58)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:113)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:121)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$2(HierarchicalTestExecutor.java:121)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.lambda$executeRecursively$3(HierarchicalTestExecutor.java:121)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.executeRecursively(HierarchicalTestExecutor.java:108)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor$NodeExecutor.execute(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:55)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
这是vertx-junit5
库上的错误吗?我还注意到,如果Verticles依赖于某些外部配置,即使我将正确的DeploymentOptions().setConfig(JsonObject().put(...))
作为第二个参数传递给deployVerticle
,它也不会读取这些参数。
答案 0 :(得分:1)
我运行了您的代码,发现执行从未达到测试方法。
您还需要使用@BeforeEach
方法关闭上下文:
@BeforeEach
fun prepare(vertx: Vertx, testContext: VertxTestContext) {
vertx.deployVerticle(DialPadVerticle(), testContext.succeeding {
testContext.completeNow()
})
}
testContext.succeeding
仅提供一个AsyncResult
处理程序,用于检查结果是否成功,但不会将整个测试上下文标记为已完成。它简化了对中介步骤的检查。
现在测试通过就可以了!
答案 1 :(得分:0)
您在某处会有一个最小的复制程序项目吗?