Jason5lee
10/10/2021, 11:27 AMIllegalArgumentException
. But don’t care about whether it is thrown at exactly the argument you make it invalid.
3. Have a unique Exception
class for every case of exceptions. E.g. every arguments may have a corresponding exception for being invalid. Then you just test whether that specific exception is thrown.
4. Test for the API response.
5. Having a field in exception which contains the details information of the exception.
6. Check message.hho
10/10/2021, 7:27 PMandylamax
10/11/2021, 12:57 AMRob Elliot
10/11/2021, 3:02 PMopen class HttpError constructor(val status: Int, message: String) : Exception("$status: $message")
open class ClientError(status: Int, message: String) : HttpError(status, message)
class NotFound(val uri: URI) : ClientError(404, "nothing found at $uri")
I would unit test `NotFound`:
NotFound(URI("<http://www.example.com/mypath>")).apply {
message shouldBe "404: nothing found at <http://www.example.com/mypath>"
status shouldBe 404
uri shouldBe URI("<http://www.example.com/mypath>")
}
and when testing a method that I expect to throw it:
val notFound = shouldThrow<NotFound> {
// code that throws it
}
notFound.uri shouldBe URI("<http://www.example.com/mypath>")