我想验证抛出我的一个运行线程的异常。这是一段我的测试代码:Spock的多线程代码测试
then:
def e = thrown(RequestFormatException)
e.message == "Incorrect first line: INCORRECT LINE"
当我运行此我得到下一个信息:
Exception in thread "Thread-1" by.westside.staircase.core.exception.RequestFormatException: Incorrect first line: INCORRECT LINE
at by.westside.staircase.core.util.HttpUtil.parseHttpRequest(HttpUtil.kt:19)
at by.westside.staircase.core.server.ServerThread.run(ServerThread.kt:26)
at java.lang.Thread.run(Thread.java:745)
Expected exception of type 'by.westside.staircase.core.exception.RequestFormatException', but no exception was thrown
at org.spockframework.lang.SpecInternals.checkExceptionThrown(SpecInternals.java:79)
at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:66)
at by.westside.staircase.core.server.SyncServerSpec.should throw exception in incorrect first line case(SyncServerSpec.groovy:26)
你能想出一个我们可以尝试的例子吗? –
@tim_yates你可以在任何由新Thread创建的线程中抛出异常(new RunnableClass())。start() – Cortwave
为什么不测试runnable而不是一个线程你还没有引用 –