当前一个方法调用引发异常时,我需要验证未进行特定的调用。
CREATE VIEW view_name AS
SELECT *
FROM table_name
PIVOT (
MAX( descr )
FOR lan IN (
'C' AS Spanish,
'V' AS English
)
)
此代码有问题,它因引发异常而不是由于验证失败而失败。
答案 0 :(得分:2)
我知道您的 WHEN 块将始终引发异常。 在这种情况下,从我的角度来看,您有多种选择:
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
var exceptionThrown: Boolean = false
try {
sut.live()
} catch(exception: NotHungryException) {
// Maybe put some assertions on the exception here.
exceptionThrown = true
}
assertTrue(exceptionThrown)
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
assertThrows
将期望特定代码段引发异常。如果没有抛出异常,它将无法通过测试。它还会返回抛出的异常,供您检查。import org.junit.jupiter.api.Assertions
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
val exception = Assertions.assertThrows(NotHungryException::class.java) { sut.live() }
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }
shouldThrow
断言。这还允许您检索抛出的异常并验证其类型。import io.kotest.assertions.throwables.shouldThrow
// GIVEN
every { relaxedMock.eats() }.throws(NotHungryException())
// WHEN
val exception = shouldThrow<NotHungryException> { sut.live() }
// THEN
verify (exactly = 0) { relaxedMock2.sleeps() }