Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 我想检查是否引发了异常,但发生了错误_Kotlin - Fatal编程技术网

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.