Kotlin 我想检查是否引发了异常,但发生了错误
我听不到例外 我的测试:Kotlin 我想检查是否引发了异常,但发生了错误,kotlin,Kotlin,我听不到例外 我的测试: @Test fun shouldException() { assertThrows<WrongArgumentsException> { val request = HttpEntity<String>("{\"username\": \"example\", \"password\": \"12345\"") val responseEntity = restTemplate.postForEntit
@Test
fun shouldException() {
assertThrows<WrongArgumentsException> {
val request = HttpEntity<String>("{\"username\": \"example\", \"password\": \"12345\"")
val responseEntity = restTemplate.postForEntity("http://localhost:$port/api/user/registration", request, String::class.java)
}
}
在调试中:
2020-06-05 17:39:43.214 DEBUG 11308 --- [o-auto-1-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Resolved [com.example.utils.exceptions.basic.WrongArgumentsException: Password should be from 6 to 32 length]
有一个例外!但有一个错误:
org.opentest4j.AssertionFailedError: Expected com.example.utils.exceptions.basic.WrongArgumentsException to be thrown, but nothing was thrown.
我认为这是因为postForEntity()调用是异步的,测试不会等待调用的结果
要进行检查,可以将对postForEntity()的调用替换为直接引发异常。然后将满足测试中的断言。我认为这是因为postForEntity()调用是异步的,测试不会等待调用的结果 要进行检查,可以将对postForEntity()的调用替换为直接引发异常。然后将满足测试中的断言
org.opentest4j.AssertionFailedError: Expected com.example.utils.exceptions.basic.WrongArgumentsException to be thrown, but nothing was thrown.