我正在进行以下测试:
class FooTest extends SpecWithJUnit with MockServer {
"Foo" should {
"fail" in new ctx {
givenAListenerThatAlwaysFailsWhenExcecuted()
foo.runMethodThatInvokesAListener
}
}
}
trait MockServer extends BeforeAfterAll with MustMatchers {
private val wireMockServer = new WireMockServer(wireMockConfig().port(9000))
def givenAListenerThatAlwaysFailsWhenExcecuted() = {
val listener = new RequestListener {
override def requestReceived(request: Request, response: Response): Unit = {
true must beFalse
}
}
wireMockServer.addMockServiceRequestListener(listener)
listener
}
override def beforeAll(): Unit = wireMockServer.start()
override def afterAll(): Unit = wireMockServer.stop()
}
当我运行它时,我在控制台中看到异常,但测试本身并没有失败。我如何让它失败?
答案 0 :(得分:1)
您必须使用MustThrownMatchers
代替MustMatchers
,以便true must beFalse
之类的任何失败预期都会被new ctx { ... }
抛出。
否则true must beFalse
只是一个简单的MatchResult[Boolean]
值,可以在匿名ctx
类的正文中创建。