How to use exceptionToMessage class of io.kotest.assertions package

Best Kotest code snippet using io.kotest.assertions.exceptionToMessage

MonitorControllerTest.kt

Source:MonitorControllerTest.kt Github

copy

Full Screen

...15import com.kuvaszuptime.kuvasz.repositories.UptimeEventRepository16import com.kuvaszuptime.kuvasz.services.CheckScheduler17import com.kuvaszuptime.kuvasz.testutils.shouldBe18import com.kuvaszuptime.kuvasz.util.getCurrentTimestamp19import io.kotest.assertions.exceptionToMessage20import io.kotest.assertions.throwables.shouldThrow21import io.kotest.core.test.TestCase22import io.kotest.core.test.TestResult23import io.kotest.inspectors.forNone24import io.kotest.inspectors.forOne25import io.kotest.matchers.collections.shouldBeEmpty26import io.kotest.matchers.collections.shouldHaveSize27import io.kotest.matchers.shouldBe28import io.kotest.matchers.shouldNotBe29import io.kotest.matchers.string.shouldContain30import io.micronaut.http.HttpRequest31import io.micronaut.http.HttpStatus32import io.micronaut.http.client.annotation.Client33import io.micronaut.http.client.exceptions.HttpClientResponseException34import io.micronaut.rxjava2.http.client.RxHttpClient35import io.micronaut.test.extensions.kotest.annotation.MicronautTest36@Suppress("LongParameterList")37@MicronautTest38class MonitorControllerTest(39 @Client("/") private val client: RxHttpClient,40 private val monitorClient: MonitorClient,41 private val monitorRepository: MonitorRepository,42 private val latencyLogRepository: LatencyLogRepository,43 private val uptimeEventRepository: UptimeEventRepository,44 private val sslEventRepository: SSLEventRepository,45 private val checkScheduler: CheckScheduler46) : DatabaseBehaviorSpec() {47 init {48 given("MonitorController's getMonitorsWithDetails() endpoint") {49 `when`("there is a monitor in the database") {50 val monitor = createMonitor(monitorRepository, pagerdutyIntegrationKey = "something")51 latencyLogRepository.insertLatencyForMonitor(monitor.id, 1200)52 latencyLogRepository.insertLatencyForMonitor(monitor.id, 600)53 latencyLogRepository.insertLatencyForMonitor(monitor.id, 600)54 val now = getCurrentTimestamp()55 createUptimeEventRecord(56 repository = uptimeEventRepository,57 monitorId = monitor.id,58 startedAt = now,59 status = UptimeStatus.UP,60 endedAt = null61 )62 createSSLEventRecord(63 repository = sslEventRepository,64 monitorId = monitor.id,65 startedAt = now,66 endedAt = null67 )68 val response = monitorClient.getMonitorsWithDetails(enabledOnly = null)69 then("it should return them") {70 response shouldHaveSize 171 val responseItem = response.first()72 responseItem.id shouldBe monitor.id73 responseItem.name shouldBe monitor.name74 responseItem.url.toString() shouldBe monitor.url75 responseItem.enabled shouldBe monitor.enabled76 responseItem.enabled shouldBe monitor.sslCheckEnabled77 responseItem.averageLatencyInMs shouldBe 80078 responseItem.p95LatencyInMs shouldBe 120079 responseItem.p99LatencyInMs shouldBe 120080 responseItem.uptimeStatus shouldBe UptimeStatus.UP81 responseItem.uptimeStatusStartedAt shouldBe now82 responseItem.uptimeError shouldBe null83 responseItem.lastUptimeCheck shouldBe now84 responseItem.createdAt shouldBe monitor.createdAt85 responseItem.sslStatus shouldBe SslStatus.VALID86 responseItem.sslStatusStartedAt shouldBe now87 responseItem.lastSSLCheck shouldBe now88 responseItem.sslError shouldBe null89 responseItem.pagerdutyKeyPresent shouldBe true90 }91 }92 `when`("enabledOnly parameter is set to true") {93 createMonitor(monitorRepository, enabled = false, monitorName = "name1")94 val enabledMonitor = createMonitor(monitorRepository, id = 11111, monitorName = "name2")95 val response = monitorClient.getMonitorsWithDetails(enabledOnly = true)96 then("it should not return disabled monitor") {97 response shouldHaveSize 198 val responseItem = response.first()99 responseItem.id shouldBe enabledMonitor.id100 responseItem.name shouldBe enabledMonitor.name101 responseItem.url.toString() shouldBe enabledMonitor.url102 responseItem.enabled shouldBe enabledMonitor.enabled103 responseItem.sslCheckEnabled shouldBe enabledMonitor.sslCheckEnabled104 responseItem.averageLatencyInMs shouldBe null105 responseItem.uptimeStatus shouldBe null106 responseItem.sslStatus shouldBe null107 responseItem.createdAt shouldBe enabledMonitor.createdAt108 responseItem.pagerdutyKeyPresent shouldBe false109 }110 }111 `when`("there isn't any monitor in the database") {112 val response = monitorClient.getMonitorsWithDetails(enabledOnly = false)113 then("it should return an empty list") {114 response shouldHaveSize 0115 }116 }117 }118 given("MonitorController's getMonitorDetails() endpoint") {119 `when`("there is a monitor with the given ID in the database") {120 val monitor = createMonitor(monitorRepository, pagerdutyIntegrationKey = "something")121 latencyLogRepository.insertLatencyForMonitor(monitor.id, 1200)122 latencyLogRepository.insertLatencyForMonitor(monitor.id, 600)123 latencyLogRepository.insertLatencyForMonitor(monitor.id, 600)124 val now = getCurrentTimestamp()125 createUptimeEventRecord(126 repository = uptimeEventRepository,127 monitorId = monitor.id,128 startedAt = now,129 status = UptimeStatus.UP,130 endedAt = null131 )132 createSSLEventRecord(133 repository = sslEventRepository,134 monitorId = monitor.id,135 startedAt = now,136 endedAt = null137 )138 then("it should return it") {139 val response = monitorClient.getMonitorDetails(monitorId = monitor.id)140 response.id shouldBe monitor.id141 response.name shouldBe monitor.name142 response.url.toString() shouldBe monitor.url143 response.enabled shouldBe monitor.enabled144 response.sslCheckEnabled shouldBe monitor.sslCheckEnabled145 response.averageLatencyInMs shouldBe 800146 response.uptimeStatus shouldBe UptimeStatus.UP147 response.createdAt shouldBe monitor.createdAt148 response.lastUptimeCheck shouldBe now149 response.sslStatus shouldBe SslStatus.VALID150 response.sslStatusStartedAt shouldBe now151 response.lastSSLCheck shouldBe now152 response.sslError shouldBe null153 response.pagerdutyKeyPresent shouldBe true154 }155 }156 `when`("there is no monitor with the given ID in the database") {157 val response = shouldThrow<HttpClientResponseException> {158 client.toBlocking().exchange<Any>("/monitors/1232132432")159 }160 then("it should return a 404") {161 response.status shouldBe HttpStatus.NOT_FOUND162 }163 }164 }165 given("MonitorController's createMonitor() endpoint") {166 `when`("it is called with a valid DTO") {167 val monitorToCreate = MonitorCreateDto(168 name = "test_monitor",169 url = "https://valid-url.com",170 uptimeCheckInterval = 6000,171 pagerdutyIntegrationKey = "something"172 )173 val createdMonitor = monitorClient.createMonitor(monitorToCreate)174 then("it should create a monitor and also schedule checks for it") {175 val monitorInDb = monitorRepository.findById(createdMonitor.id)!!176 monitorInDb.name shouldBe createdMonitor.name177 monitorInDb.url shouldBe createdMonitor.url178 monitorInDb.uptimeCheckInterval shouldBe createdMonitor.uptimeCheckInterval179 monitorInDb.enabled shouldBe createdMonitor.enabled180 monitorInDb.createdAt shouldBe createdMonitor.createdAt181 monitorInDb.pagerdutyIntegrationKey shouldBe monitorToCreate.pagerdutyIntegrationKey182 checkScheduler.getScheduledChecks().filter { it.monitorId == createdMonitor.id }183 .forOne { it.checkType shouldBe CheckType.UPTIME }184 }185 }186 `when`("there is already a monitor with the same name") {187 val firstMonitor = MonitorCreateDto(188 name = "test_monitor",189 url = "https://valid-url.com",190 uptimeCheckInterval = 6000,191 enabled = true192 )193 val secondMonitor = MonitorCreateDto(194 name = firstMonitor.name,195 url = "https://valid-url2.com",196 uptimeCheckInterval = 4000,197 enabled = false198 )199 val firstCreatedMonitor = monitorClient.createMonitor(firstMonitor)200 val secondRequest = HttpRequest.POST("/monitors", secondMonitor)201 val secondResponse = shouldThrow<HttpClientResponseException> {202 client.toBlocking().exchange<MonitorCreateDto, Any>(secondRequest)203 }204 then("it should return a 409") {205 secondResponse.status shouldBe HttpStatus.CONFLICT206 val monitorsInDb = monitorRepository.fetchByName(firstCreatedMonitor.name)207 monitorsInDb shouldHaveSize 1208 checkScheduler.getScheduledChecks().filter { it.monitorId == firstCreatedMonitor.id }209 .forOne { it.checkType shouldBe CheckType.UPTIME }210 }211 }212 `when`("it is called with an invalid URL") {213 val monitorToCreate = MonitorCreateDto(214 name = "test_monitor",215 url = "htt://invalid-url.com",216 uptimeCheckInterval = 6000,217 enabled = true218 )219 val request = HttpRequest.POST("/monitors", monitorToCreate)220 val response = shouldThrow<HttpClientResponseException> {221 client.toBlocking().exchange<MonitorCreateDto, Any>(request)222 }223 then("it should return a 400") {224 response.status shouldBe HttpStatus.BAD_REQUEST225 exceptionToMessage(response) shouldContain "url: must match \"^(https?)"226 }227 }228 `when`("it is called with an invalid uptime check interval") {229 val monitorToCreate = MonitorCreateDto(230 name = "test_monitor",231 url = "https://valid-url.com",232 uptimeCheckInterval = 59,233 enabled = true234 )235 val request = HttpRequest.POST("/monitors", monitorToCreate)236 val response = shouldThrow<HttpClientResponseException> {237 client.toBlocking().exchange<MonitorCreateDto, Any>(request)238 }239 then("it should return a 400") {240 response.status shouldBe HttpStatus.BAD_REQUEST241 exceptionToMessage(response) shouldContain242 "uptimeCheckInterval: must be greater than or equal to 60"243 }244 }245 }246 given("MonitorController's deleteMonitor() endpoint") {247 `when`("it is called with an existing monitor ID") {248 val monitorToCreate = MonitorCreateDto(249 name = "test_monitor",250 url = "https://valid-url.com",251 uptimeCheckInterval = 6000,252 enabled = true253 )254 val createdMonitor = monitorClient.createMonitor(monitorToCreate)255 val deleteRequest = HttpRequest.DELETE<Any>("/monitors/${createdMonitor.id}")...

Full Screen

Full Screen

TelegramEventHandlerConfigTest.kt

Source:TelegramEventHandlerConfigTest.kt Github

copy

Full Screen

1package com.kuvaszuptime.kuvasz.config2import io.kotest.assertions.exceptionToMessage3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.BehaviorSpec5import io.kotest.matchers.string.shouldContain6import io.micronaut.context.ApplicationContext7import io.micronaut.context.env.PropertySource8import io.micronaut.context.exceptions.BeanInstantiationException9class TelegramEventHandlerConfigTest : BehaviorSpec(10 {11 given("a TelegramEventHandlerConfig bean") {12 `when`("there is no API token in the configuration") {13 val properties = PropertySource.of(14 "test",15 mapOf(16 "handler-config.telegram-event-handler.enabled" to "true",17 "handler-config.telegram-event-handler.chat-id" to "chat-id"18 )19 )20 then("ApplicationContext should throw a BeanInstantiationException") {21 val exception = shouldThrow<BeanInstantiationException> {22 ApplicationContext.run(properties)23 }24 exceptionToMessage(exception) shouldContain25 "Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] " +26 "could not be loaded"27 }28 }29 `when`("there is no chat ID in the configuration") {30 val properties = PropertySource.of(31 "test",32 mapOf(33 "handler-config.telegram-event-handler.enabled" to "true",34 "handler-config.telegram-event-handler.token" to "your-token"35 )36 )37 then("ApplicationContext should throw a BeanInstantiationException") {38 val exception = shouldThrow<BeanInstantiationException> {39 ApplicationContext.run(properties)40 }41 exceptionToMessage(exception) shouldContain42 "Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] " +43 "could not be loaded"44 }45 }46 `when`("chat ID and API token are empty strings") {47 val properties = PropertySource.of(48 "test",49 mapOf(50 "handler-config.telegram-event-handler.enabled" to "true",51 "handler-config.telegram-event-handler.token" to "",52 "handler-config.telegram-event-handler.chat-id" to ""53 )54 )55 then("ApplicationContext should throw a BeanInstantiationException") {56 val exception = shouldThrow<BeanInstantiationException> {57 ApplicationContext.run(properties)58 }59 exceptionToMessage(exception) shouldContain60 "Bean definition [com.kuvaszuptime.kuvasz.handlers.TelegramEventHandler] " +61 "could not be loaded"62 }63 }64 }65 }66)...

Full Screen

Full Screen

AdminAuthConfigTest.kt

Source:AdminAuthConfigTest.kt Github

copy

Full Screen

1package com.kuvaszuptime.kuvasz.config2import io.kotest.assertions.exceptionToMessage3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.BehaviorSpec5import io.kotest.matchers.string.shouldContain6import io.micronaut.context.ApplicationContext7import io.micronaut.context.env.PropertySource8import io.micronaut.context.exceptions.BeanInstantiationException9class AdminAuthConfigTest : BehaviorSpec(10 {11 given("an AdminAuthConfig bean") {12 `when`("password is less than 12 characters long") {13 val properties = PropertySource.of(14 "test",15 mapOf(16 "admin-auth.username" to "test-user",17 "admin-auth.password" to "tooShortPas"18 )19 )20 then("ApplicationContext should throw a BeanInstantiationException") {21 val exception = shouldThrow<BeanInstantiationException> {22 ApplicationContext.run(properties)23 }24 exceptionToMessage(exception) shouldContain "password - size must be between 12"25 }26 }27 `when`("username or password is blank") {28 val properties = PropertySource.of(29 "test",30 mapOf(31 "admin-auth.username" to "",32 "admin-auth.password" to ""33 )34 )35 then("ApplicationContext should throw a BeanInstantiationException") {36 val exception = shouldThrow<BeanInstantiationException> {37 ApplicationContext.run(properties)38 }39 exceptionToMessage(exception) shouldContain "username - must not be blank"40 exceptionToMessage(exception) shouldContain "password - must not be blank"41 }42 }43 }44 }45)...

Full Screen

Full Screen

AppConfigTest.kt

Source:AppConfigTest.kt Github

copy

Full Screen

1package com.kuvaszuptime.kuvasz.config2import io.kotest.assertions.exceptionToMessage3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.BehaviorSpec5import io.kotest.matchers.string.shouldContain6import io.micronaut.context.ApplicationContext7import io.micronaut.context.env.PropertySource8import io.micronaut.context.exceptions.BeanInstantiationException9class AppConfigTest : BehaviorSpec(10 {11 given("an AppConfig bean") {12 `when`("there is a data-retention-days parameter with a null value") {13 val properties = PropertySource.of(14 "test",15 mapOf(16 "app-config.data-retention-days" to "null"17 )18 )19 then("ApplicationContext should throw a BeanInstantiationException") {20 val exception = shouldThrow<BeanInstantiationException> {21 ApplicationContext.run(properties)22 }23 exceptionToMessage(exception) shouldContain24 "Error resolving property value [app-config.data-retention-days]"25 }26 }27 `when`("there is a data-retention-days parameter with an exceptionally low value") {28 val properties = PropertySource.of(29 "test",30 mapOf(31 "app-config.data-retention-days" to "6"32 )33 )34 then("ApplicationContext should throw a BeanInstantiationException") {35 val exception = shouldThrow<BeanInstantiationException> {36 ApplicationContext.run(properties)37 }38 exceptionToMessage(exception) shouldContain "dataRetentionDays - must be greater than or equal to 7"39 }40 }41 }42 }43)...

Full Screen

Full Screen

error.kt

Source:error.kt Github

copy

Full Screen

1package com.github.shwaka.kotest.inspectors2import io.kotest.assertions.AssertionsConfig3import io.kotest.assertions.exceptionToMessage4import io.kotest.assertions.failure5import io.kotest.assertions.show.show6import io.kotest.inspectors.ElementFail7import io.kotest.inspectors.ElementPass8import io.kotest.inspectors.ElementResult9/**10 * Build assertion error message.11 *12 * Show 10 passed and failed results by default. You can change the number of output results by setting the13 * system property `kotest.assertions.output.max=20`.14 *15 * E.g.:16 *17 * ```18 * -Dkotest.assertions.output.max=2019 * ```20 */21fun <T> buildAssertionError(msg: String, results: List<ElementResult<T>>): Nothing {22 val maxResults = AssertionsConfig.maxErrorsOutput23 val passed = results.filterIsInstance<ElementPass<T>>()24 val failed = results.filterIsInstance<ElementFail<T>>()25 val builder = StringBuilder(msg)26 builder.append("\n\nThe following elements passed:\n")27 if (passed.isEmpty()) {28 builder.append("--none--")29 } else {30 builder.append(passed.take(maxResults).map { it.t }.joinToString("\n"))31 if (passed.size > maxResults) {32 builder.append("\n... and ${passed.size - maxResults} more passed elements")33 }34 }35 builder.append("\n\nThe following elements failed:\n")36 if (failed.isEmpty()) {37 builder.append("--none--")38 } else {39 builder.append(40 failed.take(maxResults).joinToString("\n") {41 val message = exceptionToMessage(it.throwable).lines().joinToString("\n ")42 it.t.show().value + " => " + message43 }44 )45 if (failed.size > maxResults) {46 builder.append("\n... and ${failed.size - maxResults} more failed elements")47 }48 }49 throw failure(builder.toString())50}...

Full Screen

Full Screen

SMTPMailerConfigTest.kt

Source:SMTPMailerConfigTest.kt Github

copy

Full Screen

1package com.kuvaszuptime.kuvasz.config2import io.kotest.assertions.exceptionToMessage3import io.kotest.assertions.throwables.shouldThrow4import io.kotest.core.spec.style.BehaviorSpec5import io.kotest.matchers.string.shouldContain6import io.micronaut.context.ApplicationContext7import io.micronaut.context.env.PropertySource8import io.micronaut.context.exceptions.BeanInstantiationException9class SMTPMailerConfigTest : BehaviorSpec(10 {11 given("an SMTPMailerConfig bean") {12 `when`("the SMTP host does not exists") {13 val properties = PropertySource.of(14 "test",15 mapOf(16 "handler-config.smtp-event-handler.enabled" to "true",17 "smtp-config.host" to "localhost",18 "smtp-config.port" to "123"19 )20 )21 then("ApplicationContext should throw a BeanInstantiationException") {22 val exception = shouldThrow<BeanInstantiationException> {23 ApplicationContext.run(properties)24 }25 exceptionToMessage(exception) shouldContain "Error when trying to open connection to the server"26 }27 }28 }29 }30)...

Full Screen

Full Screen

exceptionToMessage.kt

Source:exceptionToMessage.kt Github

copy

Full Screen

...3 * Returns a string error message from the given throwable.4 * If the type is an [AssertionError] then the message is taken from the exceptions own message,5 * otherwise the exception is converted to a string.6 */7fun exceptionToMessage(t: Throwable): String =8 when (t) {9 is AssertionError -> when (t.message) {10 null -> t.toString()11 else -> t.message!!12 }13 else -> t.toString()14 }...

Full Screen

Full Screen

exceptionToMessage

Using AI Code Generation

copy

Full Screen

1import io.kotest.assertions.exceptionToMessage2assertThrows<IllegalArgumentException> {3throw IllegalArgumentException("error")4}.message shouldBe exceptionToMessage { "error" }5}6}7at io.kotest.assertions.throwables.shouldThrow(throwables.kt:35)8at io.kotest.assertions.throwables.shouldThrow(throwables.kt:30)9at io.kotest.assertions.throwables.shouldThrow(throwables.kt:27)10at io.kotest.assertions.throwables.shouldThrow(throwables.kt:24)11at io.kotest.assertions.throwables.shouldThrow$default(throwables.kt:23)12at io.kotest.assertions.throwables.shouldThrow(throwables.kt:22)13at io.kotest.assertions.throwables.shouldThrow(throwables.kt:21)14at io.kotest.assertions.throwables.shouldThrow(throwables.kt:20)15at io.kotest.assertions.throwables.shouldThrow$default(throwables.kt:19)16at io.kotest.assertions.throwables.shouldThrow(throwables.kt:18)17at io.kotest.assertions.throwables.shouldThrow(throwables.kt:17)18at io.kotest.assertions.throwables.shouldThrow(throwables.kt:16)19at io.kotest.assertions.throwables.shouldThrow$default(throwables.kt:15)20at io.kotest.assertions.throwables.shouldThrow(throwables.kt:14)21at io.kotest.assertions.throwables.shouldThrow(throwables.kt:13)22at io.kotest.assertions.throwables.shouldThrow(throwables.kt:12)23at io.kotest.assertions.throwables.shouldThrow$default(throwables.kt:11)24at io.kotest.assertions.throwables.shouldThrow(throwables.kt:10)25at io.kotest.assertions.throwables.shouldThrow(throwables.kt:9)26at io.kotest.assertions.throwables.shouldThrow(throwables.kt:8)27at io.kotest.assertions.throwables.shouldThrow$default(throwables.kt:7)28at io.kotest.assertions.throwables.shouldThrow(throwables.kt:

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in exceptionToMessage

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful