How to use Throwable.shouldHaveCauseInstanceOf method of io.kotest.matchers.throwable.matchers class

Best Kotest code snippet using io.kotest.matchers.throwable.matchers.Throwable.shouldHaveCauseInstanceOf

ThrowableMatchersTest.kt

Source:ThrowableMatchersTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.throwable2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.assertions.throwables.shouldThrowAny4import io.kotest.assertions.throwables.shouldThrowExactly5import io.kotest.assertions.throwables.shouldThrowWithMessage6import io.kotest.core.spec.style.FreeSpec7import io.kotest.matchers.throwable.shouldHaveCause8import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf9import io.kotest.matchers.throwable.shouldHaveCauseOfType10import io.kotest.matchers.throwable.shouldHaveMessage11import io.kotest.matchers.throwable.shouldNotHaveCause12import io.kotest.matchers.throwable.shouldNotHaveCauseInstanceOf13import io.kotest.matchers.throwable.shouldNotHaveCauseOfType14import io.kotest.matchers.throwable.shouldNotHaveMessage15import java.io.FileNotFoundException16import java.io.IOException17class ThrowableMatchersTest : FreeSpec() {18 init {19 "shouldThrowAny" - {20 "shouldHaveMessage" {21 shouldThrowAny { throw FileNotFoundException("this_file.txt not found") } shouldHaveMessage "this_file.txt not found"22 shouldThrowAny { throw TestException() } shouldHaveMessage "This is a test exception"23 shouldThrowAny { throw CompleteTestException() } shouldHaveMessage "This is a complete test exception"24 }25 "shouldNotHaveMessage" {26 shouldThrowAny { throw FileNotFoundException("this_file.txt not found") } shouldNotHaveMessage "random message"27 shouldThrowAny { throw TestException() } shouldNotHaveMessage "This is a complete test exception"28 shouldThrowAny { throw CompleteTestException() } shouldNotHaveMessage "This is a test exception"29 }30 "shouldHaveCause" {31 shouldThrowAny { throw CompleteTestException() }.shouldHaveCause()32 shouldThrowAny { throw CompleteTestException() }.shouldHaveCause {33 it shouldHaveMessage "file.txt not found"34 }35 }36 "shouldNotHaveCause" {37 shouldThrowAny { throw TestException() }.shouldNotHaveCause()38 shouldThrowAny { throw FileNotFoundException("this_file.txt not found") }.shouldNotHaveCause()39 }40 "shouldHaveCauseInstanceOf" {41 shouldThrowAny { throw CompleteTestException() }.shouldHaveCauseInstanceOf<FileNotFoundException>()42 shouldThrowAny { throw CompleteTestException() }.shouldHaveCauseInstanceOf<IOException>()43 }44 "shouldNotHaveCauseInstanceOf" {45 shouldThrowAny { throw CompleteTestException() }.shouldNotHaveCauseInstanceOf<TestException>()46 }47 "shouldHaveCauseOfType" {48 shouldThrowAny { throw CompleteTestException() }.shouldHaveCauseOfType<FileNotFoundException>()49 }50 "shouldNotHaveCauseOfType" {51 shouldThrowAny { throw CompleteTestException() }.shouldNotHaveCauseOfType<IOException>()52 }53 }54 "shouldThrow" - {55 "shouldHaveMessage" {56 shouldThrow<IOException> { throw FileNotFoundException("this_file.txt not found") } shouldHaveMessage "this_file.txt not found"57 shouldThrow<TestException> { throw TestException() } shouldHaveMessage "This is a test exception"58 shouldThrow<CompleteTestException> { throw CompleteTestException() } shouldHaveMessage "This is a complete test exception"59 shouldThrow<AssertionError> { TestException() shouldHaveMessage "foo" }60 .shouldHaveMessage(61 """Throwable should have message:62"foo"63Actual was:64"This is a test exception"65expected:<"foo"> but was:<"This is a test exception">"""66 )67 }68 "shouldNotHaveMessage" {69 shouldThrow<IOException> { throw FileNotFoundException("this_file.txt not found") } shouldNotHaveMessage "random message"70 shouldThrow<TestException> { throw TestException() } shouldNotHaveMessage "This is a complete test exception"71 shouldThrow<CompleteTestException> { throw CompleteTestException() } shouldNotHaveMessage "This is a test exception"72 shouldThrow<AssertionError> { TestException() shouldNotHaveMessage "This is a test exception" }73 .shouldHaveMessage("Throwable should not have message:\n\"This is a test exception\"")74 }75 "shouldHaveCause" {76 shouldThrow<CompleteTestException> { throw CompleteTestException() }.shouldHaveCause()77 shouldThrow<CompleteTestException> { throw CompleteTestException() }.shouldHaveCause {78 it shouldHaveMessage "file.txt not found"79 }80 shouldThrow<AssertionError> { TestException().shouldHaveCause() }81 .shouldHaveMessage("Throwable should have a cause")82 }83 "shouldNotHaveCause" {84 shouldThrow<TestException> { throw TestException() }.shouldNotHaveCause()85 shouldThrow<IOException> { throw FileNotFoundException("this_file.txt not found") }.shouldNotHaveCause()86 shouldThrow<AssertionError> { CompleteTestException().shouldNotHaveCause() }87 .shouldHaveMessage("Throwable should not have a cause")88 }89 "shouldHaveCauseInstanceOf" {90 shouldThrow<CompleteTestException> { throw CompleteTestException() }.shouldHaveCauseInstanceOf<FileNotFoundException>()91 shouldThrow<CompleteTestException> { throw CompleteTestException() }.shouldHaveCauseInstanceOf<IOException>()92 shouldThrow<AssertionError> { CompleteTestException().shouldHaveCauseInstanceOf<RuntimeException>() }93 .shouldHaveMessage("Throwable cause should be of type java.lang.RuntimeException or it's descendant, but instead got java.io.FileNotFoundException")94 }95 "shouldNotHaveCauseInstanceOf" {96 shouldThrow<CompleteTestException> { throw CompleteTestException() }.shouldNotHaveCauseInstanceOf<TestException>()97 shouldThrow<AssertionError> { CompleteTestException().shouldNotHaveCauseInstanceOf<FileNotFoundException>() }98 .shouldHaveMessage("Throwable cause should not be of type java.io.FileNotFoundException or it's descendant")99 }100 "shouldHaveCauseOfType" {101 shouldThrow<CompleteTestException> { throw CompleteTestException() }.shouldHaveCauseOfType<FileNotFoundException>()102 shouldThrow<AssertionError> { CompleteTestException().shouldHaveCauseOfType<RuntimeException>() }103 .shouldHaveMessage("Throwable cause should be of type java.lang.RuntimeException, but instead got java.io.FileNotFoundException")104 }105 "shouldNotHaveCauseOfType" {106 shouldThrow<CompleteTestException> { throw CompleteTestException() }.shouldNotHaveCauseOfType<IOException>()107 shouldThrow<AssertionError> { CompleteTestException().shouldNotHaveCauseOfType<FileNotFoundException>() }108 .shouldHaveMessage("Throwable cause should not be of type java.io.FileNotFoundException")109 }110 }111 "shouldThrowExactly" - {112 "shouldHaveMessage" {113 shouldThrowExactly<FileNotFoundException> { throw FileNotFoundException("this_file.txt not found") } shouldHaveMessage "this_file.txt not found"114 shouldThrowExactly<TestException> { throw TestException() } shouldHaveMessage "This is a test exception"115 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() } shouldHaveMessage "This is a complete test exception"116 }117 "shouldNotHaveMessage" {118 shouldThrowExactly<FileNotFoundException> { throw FileNotFoundException("this_file.txt not found") } shouldNotHaveMessage "random message"119 shouldThrowExactly<TestException> { throw TestException() } shouldNotHaveMessage "This is a complete test exception"120 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() } shouldNotHaveMessage "This is a test exception"121 }122 "shouldHaveCause" {123 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() }.shouldHaveCause()124 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() }.shouldHaveCause {125 it shouldHaveMessage "file.txt not found"126 }127 }128 "shouldNotHaveCause" {129 shouldThrowExactly<TestException> { throw TestException() }.shouldNotHaveCause()130 shouldThrowExactly<FileNotFoundException> { throw FileNotFoundException("this_file.txt not found") }.shouldNotHaveCause()131 }132 "shouldHaveCauseInstanceOf" {133 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() }.shouldHaveCauseInstanceOf<FileNotFoundException>()134 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() }.shouldHaveCauseInstanceOf<IOException>()135 }136 "shouldNotHaveCauseInstanceOf" {137 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() }.shouldNotHaveCauseInstanceOf<TestException>()138 }139 "shouldHaveCauseOfType" {140 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() }.shouldHaveCauseOfType<FileNotFoundException>()141 }142 "shouldNotHaveCauseOfType" {143 shouldThrowExactly<CompleteTestException> { throw CompleteTestException() }.shouldNotHaveCauseOfType<IOException>()144 }145 }146 "result" - {147 "shouldHaveMessage" {148 Result.failure<Any>(FileNotFoundException("this_file.txt not found"))149 .exceptionOrNull()!! shouldHaveMessage "this_file.txt not found"150 Result.failure<Any>(TestException()).exceptionOrNull()!! shouldHaveMessage "This is a test exception"151 Result.failure<Any>(CompleteTestException())152 .exceptionOrNull()!! shouldHaveMessage "This is a complete test exception"153 }154 "shouldNotHaveMessage" {155 Result.failure<Any>(FileNotFoundException("this_file.txt not found"))156 .exceptionOrNull()!! shouldNotHaveMessage "random message"157 Result.failure<Any>(TestException())158 .exceptionOrNull()!! shouldNotHaveMessage "This is a complete test exception"159 Result.failure<Any>(CompleteTestException())160 .exceptionOrNull()!! shouldNotHaveMessage "This is a test exception"161 }162 "shouldHaveCause" {163 Result.failure<Any>(CompleteTestException()).exceptionOrNull()!!.shouldHaveCause()164 Result.failure<Any>(CompleteTestException()).exceptionOrNull()!!.shouldHaveCause {165 it shouldHaveMessage "file.txt not found"166 }167 }168 "shouldNotHaveCause" {169 Result.failure<Any>(TestException()).exceptionOrNull()!!.shouldNotHaveCause()170 Result.failure<Any>(FileNotFoundException("this_file.txt not found")).exceptionOrNull()!!171 .shouldNotHaveCause()172 }173 "shouldHaveCauseInstanceOf" {174 Result.failure<Any>(CompleteTestException()).exceptionOrNull()!!175 .shouldHaveCauseInstanceOf<FileNotFoundException>()176 Result.failure<Any>(CompleteTestException()).exceptionOrNull()!!.shouldHaveCauseInstanceOf<IOException>()177 }178 "shouldNotHaveCauseInstanceOf" {179 Result.failure<Any>(CompleteTestException()).exceptionOrNull()!!180 .shouldNotHaveCauseInstanceOf<TestException>()181 }182 "shouldHaveCauseOfType" {183 Result.failure<Any>(CompleteTestException()).exceptionOrNull()!!184 .shouldHaveCauseOfType<FileNotFoundException>()185 }186 "shouldNotHaveCauseOfType" {187 Result.failure<Any>(CompleteTestException()).exceptionOrNull()!!.shouldNotHaveCauseOfType<IOException>()188 }189 }190 "shouldThrowWithMessage" {191 shouldThrowWithMessage<TestException>("This is a test exception") {192 throw TestException()193 } shouldHaveMessage "This is a test exception"194 }195 }196 class TestException : Throwable("This is a test exception")197 class CompleteTestException :198 Throwable("This is a complete test exception", FileNotFoundException("file.txt not found"))199}...

Full Screen

Full Screen

EncryptedStreamFactoryTest.kt

Source:EncryptedStreamFactoryTest.kt Github

copy

Full Screen

1/*2 * Copyright (C) 2022 Edgar Asatryan3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package io.github.nstdio.http.ext17import io.kotest.assertions.assertSoftly18import io.kotest.assertions.throwables.shouldThrowExactly19import io.kotest.matchers.shouldBe20import io.kotest.matchers.shouldNotBe21import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf22import io.kotest.matchers.throwable.shouldHaveMessage23import org.junit.jupiter.api.AfterEach24import org.junit.jupiter.api.Test25import org.junit.jupiter.api.io.TempDir26import org.junit.jupiter.params.ParameterizedTest27import org.junit.jupiter.params.provider.Arguments28import org.junit.jupiter.params.provider.Arguments.arguments29import org.junit.jupiter.params.provider.MethodSource30import org.mockito.BDDMockito.anyInt31import org.mockito.BDDMockito.given32import org.mockito.BDDMockito.times33import org.mockito.BDDMockito.verify34import org.mockito.Mockito.any35import org.mockito.Mockito.mock36import java.io.BufferedWriter37import java.io.EOFException38import java.io.IOException39import java.io.InputStream40import java.io.OutputStream41import java.io.OutputStreamWriter42import java.nio.charset.StandardCharsets.UTF_843import java.nio.file.Path44import java.security.InvalidKeyException45import java.security.Key46import java.security.NoSuchProviderException47import java.security.ProviderException48import kotlin.io.path.readText49class EncryptedStreamFactoryTest {50 @TempDir51 private lateinit var dir: Path52 @AfterEach53 fun setup() {54 EncryptedStreamFactory.clear()55 }56 @ParameterizedTest57 @MethodSource("rwData")58 fun `Should rw`(algorithm: String, publicKey: Key, privateKey: Key) {59 //given60 val delegate = SimpleStreamFactory()61 val writeFactory = EncryptedStreamFactory(delegate, publicKey, privateKey, algorithm, null)62 val readFactory = EncryptedStreamFactory(delegate, publicKey, privateKey, algorithm, null)63 val expected = "abcdefjabcdefjabcdefjabcdefjasaea"64 val path = dir.resolve("sample")65 //when66 writeFactory.output(path).toWriter().use { it.write(expected) }67 val decrypted = readFactory.input(path).use { it.readText() }68 val plain = path.readText()69 //then70 decrypted.shouldBe(expected)71 plain.shouldNotBe(expected)72 }73 @Test74 fun `Should close input when exception occures`() {75 //given76 val delegate = mock(StreamFactory::class.java)77 val mockIs = mock(InputStream::class.java)78 val mockOut = mock(OutputStream::class.java)79 val expectedIO = IOException("expected")80 val key = Crypto.pbe()81 val factory = EncryptedStreamFactory(delegate, key, key, "AES/CBC/PKCS5Padding", null)82 given(mockIs.read()).willThrow(expectedIO)83 given(mockOut.write(anyInt())).willThrow(expectedIO)84 given(delegate.input(any(), any())).willReturn(mockIs)85 given(delegate.output(any(), any())).willReturn(mockOut)86 //when + then87 shouldThrowExactly<IOException> { factory.input(Path.of("abc")) }88 .shouldHaveMessage("expected")89 shouldThrowExactly<IOException> { factory.output(Path.of("abc")) }90 .shouldHaveMessage("expected")91 verify(mockIs, times(2)).close()92 verify(mockOut).close()93 }94 @Test95 fun `Should throw when provider does not exists`() {96 //given97 val delegate = mock(StreamFactory::class.java)98 val key = Crypto.pbe()99 val factory = EncryptedStreamFactory(delegate, key, key, "AES/CBC/PKCS5Padding", "plain-text-cryptography")100 //when + then101 shouldThrowExactly<IOException> { factory.input(Path.of("any")) }102 .shouldHaveCauseInstanceOf<NoSuchProviderException>()103 shouldThrowExactly<IOException> { factory.output(Path.of("any")) }104 .shouldHaveCauseInstanceOf<NoSuchProviderException>()105 }106 @Test107 fun `Should throw when key is invalid`() {108 //given109 val delegate = mock(StreamFactory::class.java).also {110 given(it.input(any())).willReturn(InputStream.nullInputStream())111 }112 val factory = Crypto.rsaKeyPair().let {113 EncryptedStreamFactory(delegate, it.public, it.private, "AES/CBC/PKCS5Padding", null)114 }115 //when + then116 assertSoftly {117 shouldThrowExactly<IOException> { factory.output(Path.of("any")) }118 .shouldHaveCauseInstanceOf<InvalidKeyException>()119 shouldThrowExactly<IOException> { factory.input(Path.of("any")) }120 .shouldHaveCauseInstanceOf<ProviderException>()121 }122 }123 @Test124 fun `Should throw eof when cannot read header length`() {125 //given126 val delegate = mock(StreamFactory::class.java)127 given(delegate.input(any())).willReturn(InputStream.nullInputStream())128 val key = Crypto.pbe()129 val factory = EncryptedStreamFactory(delegate, key, key, "AES/CBC/PKCS5Padding", null)130 //when + then131 shouldThrowExactly<EOFException> { factory.input(Path.of("any")) }132 }133 private fun InputStream.readText(): String {134 return String(readAllBytes(), UTF_8)135 }136 private fun OutputStream.toWriter() = BufferedWriter(OutputStreamWriter(this, UTF_8))137 companion object {138 @JvmStatic139 fun rwData(): List<Arguments> {140 val key1 = Crypto.rsaKeyPair()141 val key2 = Crypto.rsaKeyPair()142 return listOf(143 arguments("AES", Crypto.pbe()),144 arguments("AES/CBC/PKCS5Padding", Crypto.pbe()),145 arguments("AES/ECB/PKCS5Padding", Crypto.pbe()),146 arguments("AES/ECB/PKCS5Padding", Crypto.pbe()),147 arguments("DES/CBC/PKCS5Padding", Crypto.pbe(algo = "DES", keyLen = 64)),148 arguments("DES/ECB/PKCS5Padding", Crypto.pbe(algo = "DES", keyLen = 64)),149 arguments("DESede/CBC/PKCS5Padding", Crypto.pbe(algo = "DESede", keyLen = 192)),150 arguments("DESede/ECB/PKCS5Padding ", Crypto.pbe(algo = "DESede", keyLen = 192)),151 arguments("RSA/ECB/PKCS1Padding", key1.public, key1.private),152 arguments("RSA", key2.public, key2.private),153 )154 .map {155 val args = it.get()156 if (args.size == 2) arguments(args[0], args[1], args[1]) else it157 }158 }159 }160}...

Full Screen

Full Screen

DynamicAddressEndpointsTest.kt

Source:DynamicAddressEndpointsTest.kt Github

copy

Full Screen

1package ru.fix.armeria.dynamic.request.endpoint2import com.linecorp.armeria.client.UnprocessedRequestException3import com.linecorp.armeria.client.WebClient4import com.linecorp.armeria.client.endpoint.EmptyEndpointGroupException5import com.linecorp.armeria.common.HttpResponse6import com.linecorp.armeria.common.HttpStatus7import com.linecorp.armeria.common.SessionProtocol8import io.kotest.assertions.throwables.shouldThrowExactly9import io.kotest.matchers.shouldBe10import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf11import kotlinx.coroutines.Deferred12import kotlinx.coroutines.awaitAll13import kotlinx.coroutines.future.asDeferred14import kotlinx.coroutines.joinAll15import org.junit.jupiter.api.Test16import org.junit.jupiter.api.TestInstance17import ru.fix.armeria.commons.testing.ArmeriaMockServer18import ru.fix.dynamic.property.api.AtomicProperty19import java.net.URI20import java.util.concurrent.atomic.AtomicInteger21@TestInstance(TestInstance.Lifecycle.PER_CLASS)22internal class DynamicAddressEndpointsTest {23 @Test24 suspend fun `dynamicAddressEndpoint WHEN property changed THEN requests are targeted to other address`() {25 val (mockServer1, mockServer1RequestsCounter) = createMockServerWithRequestsCount()26 val (mockServer2, mockServer2RequestsCounter) = createMockServerWithRequestsCount()27 try {28 listOf(29 mockServer1.launchStart(),30 mockServer2.launchStart()31 ).joinAll()32 val mockServer1Address = mockServer1.httpUri().asSocketAddress()33 val mockServer2Address = mockServer2.httpUri().asSocketAddress()34 val dynamicAddressProp = AtomicProperty<SocketAddress>(mockServer1Address)35 val dynamicAddressEndpoint = DynamicAddressEndpoints.dynamicAddressEndpoint(dynamicAddressProp)36 val client = WebClient.builder(SessionProtocol.HTTP, dynamicAddressEndpoint).build()37 client.makeCountedRequest().await()38 mockServer1RequestsCounter.get() shouldBe 139 mockServer2RequestsCounter.get() shouldBe 040 dynamicAddressProp.set(mockServer2Address)41 client.makeCountedRequest().await()42 mockServer1RequestsCounter.get() shouldBe 143 mockServer2RequestsCounter.get() shouldBe 144 dynamicAddressProp.set(mockServer1Address)45 client.makeCountedRequest().await()46 mockServer1RequestsCounter.get() shouldBe 247 mockServer2RequestsCounter.get() shouldBe 148 } finally {49 listOf(50 mockServer1.launchStop(),51 mockServer2.launchStop()52 ).joinAll()53 }54 }55 @Test56 suspend fun `dynamicAddressListEndpointGroup WHEN property changed THEN requests are targeted to other addresses`() {57 val (mockServer1, mockServer1RequestsCounter) = createMockServerWithRequestsCount()58 val (mockServer2, mockServer2RequestsCounter) = createMockServerWithRequestsCount()59 val (mockServer3, mockServer3RequestsCounter) = createMockServerWithRequestsCount()60 fun assertRequestCounters(61 expectedMockServer1RequestsCounter: Int,62 expectedMockServer2RequestsCounter: Int,63 expectedMockServer3RequestsCounter: Int64 ) {65 mockServer1RequestsCounter.get() shouldBe expectedMockServer1RequestsCounter66 mockServer2RequestsCounter.get() shouldBe expectedMockServer2RequestsCounter67 mockServer3RequestsCounter.get() shouldBe expectedMockServer3RequestsCounter68 }69 try {70 listOf(71 mockServer1.launchStart(),72 mockServer2.launchStart(),73 mockServer3.launchStart()74 ).joinAll()75 val mockServer1Address = mockServer1.httpUri().asSocketAddress()76 val mockServer2Address = mockServer2.httpUri().asSocketAddress()77 val mockServer3Address = mockServer3.httpUri().asSocketAddress()78 val dynamicAddressListProp = AtomicProperty<List<SocketAddress>>(79 listOf(80 mockServer1Address81 )82 )83 val dynamicAddressListEndpointGroup = DynamicAddressEndpoints.dynamicAddressListEndpointGroup(84 dynamicAddressListProp85 )86 val client = WebClient.builder(SessionProtocol.HTTP, dynamicAddressListEndpointGroup).build()87 List(2) {88 client.makeCountedRequest()89 }.awaitAll()90 assertRequestCounters(2, 0, 0)91 dynamicAddressListProp.set(listOf(mockServer2Address, mockServer3Address))92 List(2) {93 client.makeCountedRequest()94 }.awaitAll()95 assertRequestCounters(2, 1, 1)96 dynamicAddressListProp.set(listOf(mockServer1Address, mockServer3Address))97 List(2) {98 client.makeCountedRequest()99 }.awaitAll()100 assertRequestCounters(3, 1, 2)101 dynamicAddressListProp.set(emptyList())102 val thrownException = shouldThrowExactly<UnprocessedRequestException> {103 client.makeCountedRequest().await()104 }105 thrownException.shouldHaveCauseInstanceOf<EmptyEndpointGroupException>()106 } finally {107 listOf(108 mockServer1.launchStop(),109 mockServer2.launchStop(),110 mockServer3.launchStop()111 ).joinAll()112 }113 }114 companion object {115 const val COUNTED_PATH = "/counted"116 fun URI.asSocketAddress(): SocketAddress = SocketAddress(this.host, this.port)117 fun WebClient.makeCountedRequest(): Deferred<*> = get(COUNTED_PATH).aggregate().asDeferred()118 fun createMockServerWithRequestsCount(): Pair<ArmeriaMockServer, AtomicInteger> {119 val requestsCounter = AtomicInteger(0)120 val mockServer = ArmeriaMockServer {121 service(COUNTED_PATH) { _, _ ->122 requestsCounter.incrementAndGet()123 HttpResponse.of(HttpStatus.OK)124 }125 }126 return mockServer to requestsCounter127 }128 }129}...

Full Screen

Full Screen

TestKoTest.kt

Source:TestKoTest.kt Github

copy

Full Screen

1import com.freiheit.assertions.*2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.matchers.Matcher4import io.kotest.matchers.MatcherResult5import io.kotest.matchers.equality.shouldBeEqualToIgnoringFields6import io.kotest.matchers.equality.shouldBeEqualToUsingFields7import io.kotest.matchers.should8import io.kotest.matchers.shouldBe9import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf10import org.junit.Test11/**12 * Pros:13 * * Infix notation14 * * Impressive error messages (detection of broken fields)15 * * vast amount of assertions16 * * modules for ktor and others17 * * multiplatform support18 */19class TestKoTest {20 private val matthias = Person(21 name = "Matthias",22 lastName = "Bender"23 )24 private val somebody = Person(25 name = "Hans",26 lastName = "Wurst",27 address = Address(28 "Some Street 42",29 "123456",30 City.Hamburg31 )32 )33 @Test34 fun `test matthias has names`() {35 somebody shouldBe somebody.copy(address = somebody.address!!.copy(street = "bla"))36 matthias shouldBe Person("Matthias", "Bende123r")37 matthias.shouldBeEqualToUsingFields(38 Person("Matthias", "Bender", address = somebody.address),39 Person::name, Person::lastName40 )41 matthias.shouldBeEqualToIgnoringFields(42 Person("Matthias", "Bender", address = somebody.address),43 Person::address44 )45 }46 @Test47 fun `asserting failures`() {48 shouldThrow<CannotDieException> {49 matthias.kill()50 }.shouldHaveCauseInstanceOf<IllegalArgumentException>()51 }52 @Test53 fun `test extensions`() {54 matthias.shouldNotBeCool()55 somebody.shouldBeCool()56 }57 private fun Person.shouldBeCool(): Person {58 should {59 it.name == "Matthias"60 }61 should(CoolnessMatcher())62 return this63 }64 private fun Person.shouldNotBeCool(): Person {65 should(CoolnessMatcher().invert())66 return this67 }68 class CoolnessMatcher : Matcher<Person> {69 override fun test(value: Person): MatcherResult {70 return object : MatcherResult {71 override fun failureMessage(): String =72 "${value.name} ${value.lastName} is not cool!"73 override fun negatedFailureMessage(): String =74 "${value.name} ${value.lastName} is too cool!"75 override fun passed(): Boolean = value.name == "Matthias"76 }77 }78 }79}...

Full Screen

Full Screen

matchers.kt

Source:matchers.kt Github

copy

Full Screen

1package io.kotest.matchers.throwable2import io.kotest.assertions.show.show3import io.kotest.matchers.Matcher4import io.kotest.matchers.MatcherResult5import io.kotest.matchers.should6import io.kotest.matchers.shouldNot7infix fun Throwable.shouldHaveMessage(message: String) = this should haveMessage(message)8infix fun Throwable.shouldNotHaveMessage(message: String) = this shouldNot haveMessage(message)9fun haveMessage(message: String) = object : Matcher<Throwable> {10 override fun test(value: Throwable) = MatcherResult(11 value.message == message,12 "Throwable should have message ${message.show().value}, but instead got ${value.message.show().value}",13 "Throwable should not have message ${message.show().value}"14 )15}16fun Throwable.shouldHaveCause(block: (Throwable) -> Unit = {}) {17 this should haveCause()18 block.invoke(cause!!)19}20fun Throwable.shouldNotHaveCause() = this shouldNot haveCause()21fun haveCause() = object : Matcher<Throwable> {22 override fun test(value: Throwable) = resultForThrowable(value.cause)23}24inline fun <reified T : Throwable> Throwable.shouldHaveCauseInstanceOf() = this should haveCauseInstanceOf<T>()25inline fun <reified T : Throwable> Throwable.shouldNotHaveCauseInstanceOf() = this shouldNot haveCauseInstanceOf<T>()26inline fun <reified T : Throwable> haveCauseInstanceOf() = object : Matcher<Throwable> {27 override fun test(value: Throwable) = when {28 value.cause == null -> resultForThrowable(value.cause)29 else -> MatcherResult(30 value.cause is T,31 "Throwable cause should be of type ${T::class}, but instead got ${value::class}",32 "Throwable cause should be of type ${T::class}"33 )34 }35}36inline fun <reified T : Throwable> Throwable.shouldHaveCauseOfType() = this should haveCauseOfType<T>()37inline fun <reified T : Throwable> Throwable.shouldNotHaveCauseOfType() = this shouldNot haveCauseOfType<T>()38inline fun <reified T : Throwable> haveCauseOfType() = object : Matcher<Throwable> {39 override fun test(value: Throwable) = when (value.cause) {40 null -> resultForThrowable(value.cause)41 else -> MatcherResult(42 value.cause!!::class == T::class,43 "Throwable cause should be of type ${T::class}, but instead got ${value::class}",44 "Throwable cause should be of type ${T::class}"45 )46 }47}48@PublishedApi49internal fun resultForThrowable(value: Throwable?) = MatcherResult(50 value != null,51 "Throwable should have a cause",52 "Throwable should not have a cause"53)...

Full Screen

Full Screen

SetKeyTest.kt

Source:SetKeyTest.kt Github

copy

Full Screen

1package dev.madetobuild.typedconfig.runtime.key2import dev.madetobuild.typedconfig.runtime.ParseException3import dev.madetobuild.typedconfig.runtime.source.Source4import io.kotest.assertions.throwables.shouldThrow5import io.kotest.matchers.should6import io.kotest.matchers.shouldBe7import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf8import io.kotest.matchers.throwable.shouldHaveMessage9import io.mockk.every10import io.mockk.impl.annotations.MockK11import io.mockk.junit5.MockKExtension12import org.junit.jupiter.api.Test13import org.junit.jupiter.api.extension.ExtendWith14@ExtendWith(MockKExtension::class)15internal class SetKeyTest {16 @MockK17 lateinit var source: Source18 @Test19 fun canParseSetOfInts() {20 every { source.getList(any()) } returns listOf("1", "2", "3")21 val setKey = ListKey("setKey", source, null, emptyList()) { IntegerKey.parse(it) }22 .asSet()23 setKey.resolve() shouldBe setOf(1, 2, 3)24 }25 @Test26 fun failsIfCantParse() {27 every { source.getList(any()) } returns listOf("1", "2.0")28 val setKey = ListKey("setKey", source, null, emptyList()) { IntegerKey.parse(it) }29 .asSet()30 shouldThrow<ParseException> {31 setKey.resolve()32 } should { e ->33 e.shouldHaveMessage("Failed to parse `setKey[1]`: failed to parse '2.0'")34 e.shouldHaveCauseInstanceOf<ParseException>()35 }36 }37}...

Full Screen

Full Screen

ListKeyTest.kt

Source:ListKeyTest.kt Github

copy

Full Screen

1package dev.madetobuild.typedconfig.runtime.key2import dev.madetobuild.typedconfig.runtime.ParseException3import dev.madetobuild.typedconfig.runtime.source.Source4import io.kotest.assertions.throwables.shouldThrow5import io.kotest.matchers.should6import io.kotest.matchers.shouldBe7import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf8import io.kotest.matchers.throwable.shouldHaveMessage9import io.mockk.every10import io.mockk.impl.annotations.MockK11import io.mockk.junit5.MockKExtension12import org.junit.jupiter.api.Test13import org.junit.jupiter.api.extension.ExtendWith14@ExtendWith(MockKExtension::class)15internal class ListKeyTest {16 @MockK17 lateinit var source: Source18 @Test19 fun canParseListOfInts() {20 every { source.getList(any()) } returns listOf("1", "2", "3")21 val listKey = ListKey("listKey", source, null, emptyList()) { IntegerKey.parse(it) }22 listKey.resolve() shouldBe listOf(1, 2, 3)23 }24 @Test25 fun failsIfCantParse() {26 every { source.getList(any()) } returns listOf("1", "2.0")27 val listKey = ListKey("listKey", source, null, emptyList()) { IntegerKey.parse(it) }28 shouldThrow<ParseException> {29 listKey.resolve()30 } should { e ->31 e.shouldHaveMessage("Failed to parse `listKey[1]`: failed to parse '2.0'")32 e.shouldHaveCauseInstanceOf<ParseException>()33 }34 }35}...

Full Screen

Full Screen

Throwable.shouldHaveCauseInstanceOf

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.throwable.shouldHaveCauseInstanceOf2import io.kotest.matchers.throwable.shouldHaveMessage3import io.kotest.matchers.throwable.shouldHaveMessageContaining4import io.kotest.matchers.throwable.shouldHaveMessageStartingWith5import io.kotest.matchers.throwable.shouldHaveNoCause6import io.kotest.matchers.throwable.shouldHaveSuppressedException7import io.kotest.matchers.throwable.shouldNotHaveMessage8import io.kotest.matchers.throwable.shouldNotHaveMessageContaining9import io.kotest.matchers.throwable.shouldNotHaveMessageStartingWith10import io.kotest.matchers.throwable.shouldNotHaveSuppressedException11import io.kotest.matchers.throwable.shouldNotThrowAnyException12import io.kotest.matchers.throwable.shouldThrow13import io.kotest.matchers.throwable.shouldThrowAnyException14import io.kotest.matchers.throwable.shouldThrowAnyExceptionOfType15import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithMessage16import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithMessageContaining17import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithMessageStartingWith18import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithNoCause19import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedException20import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionOfType21import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithMessage22import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithMessageContaining23import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithMessageStartingWith24import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithNoCause25import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithSuppressedException26import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithSuppressedExceptionOfType27import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithSuppressedExceptionWithMessage28import io.kotest.matchers.throwable.shouldThrowAnyExceptionWithSuppressedExceptionWithSuppressedExceptionWithMessageContaining29import io.kotest.matchers.throwable.shouldThrowAnyException

Full Screen

Full Screen

Throwable.shouldHaveCauseInstanceOf

Using AI Code Generation

copy

Full Screen

1fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> ) : T2fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: String ) : T3fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: () -> String ) : T4fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: (T) -> String ) : T5fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: (T, KClass <out Throwable>) -> String ) : T6fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: (T, KClass <out Throwable>, Throwable) -> String ) : T7fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: (T, KClass <out Throwable>, Throwable, Throwable?) -> String ) : T8fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: (T, KClass <out Throwable>, Throwable, Throwable?, Throwable?) -> String ) : T9fun <T : Throwable> T.shouldHaveCauseInstanceOf (expected: KClass <out Throwable> , message: (T, KClass <out Throwable>, Throwable, Throwable?,

Full Screen

Full Screen

Throwable.shouldHaveCauseInstanceOf

Using AI Code Generation

copy

Full Screen

1val exception = shouldThrow<IOException> { throw IOException("boom") }2exception.shouldHaveCauseInstanceOf<IllegalArgumentException>()3Throwable exception = shouldThrow(IOException.class, () -> { throw new IOException("boom"); });4exception.shouldHaveCauseInstanceOf(IllegalArgumentException.class);5val exception = shouldThrow<IOException> { throw IOException("boom") }6exception.shouldHaveCauseInstanceOf<IllegalArgumentException>()7Throwable exception = shouldThrow(IOException.class, () -> { throw new IOException("boom"); });8exception.shouldHaveCauseInstanceOf(IllegalArgumentException.class);9val exception = shouldThrow<IOException> { throw IOException("boom") }10exception.shouldHaveCauseInstanceOf<IllegalArgumentException>()11Throwable exception = shouldThrow(IOException.class, () -> { throw new IOException("boom"); });12exception.shouldHaveCauseInstanceOf(IllegalArgumentException.class);13val exception = shouldThrow<IOException> { throw IOException("boom") }14exception.shouldHaveCauseInstanceOf<IllegalArgumentException>()15Throwable exception = shouldThrow(IOException.class, () -> { throw new IOException("boom"); });16exception.shouldHaveCauseInstanceOf(IllegalArgumentException.class);17val exception = shouldThrow<IOException> { throw IOException("boom") }18exception.shouldHaveCauseInstanceOf<IllegalArgumentException>()19Throwable exception = shouldThrow(IOException.class, () -> { throw new IOException("boom"); });20exception.shouldHaveCauseInstanceOf(IllegalArgumentException.class);

Full Screen

Full Screen

Throwable.shouldHaveCauseInstanceOf

Using AI Code Generation

copy

Full Screen

1shouldThrow<IllegalArgumentException> {2throw IllegalArgumentException("Illegal argument")3}.shouldHaveCauseInstanceOf<NullPointerException>()4}5}6at io.kotest.assertions.throwables.shouldHaveCauseInstanceOf(throwables.kt:36)7at io.kotest.assertions.throwables.shouldHaveCauseInstanceOf$default(throwables.kt:35)8at io.kotest.assertions.throwables.shouldHaveCauseInstanceOf(throwables.kt)9at com.vogella.kotlin.test.throwable.ShouldHaveCauseInstanceofKt.shouldHaveCauseInstanceof(ShouldHaveCauseInstanceof.kt:14)10at com.vogella.kotlin.test.throwable.ShouldHaveCauseInstanceofKt.main(ShouldHaveCauseInstanceof.kt:8)11at com.vogella.kotlin.test.throwable.ShouldHaveCauseInstanceofKt.main(ShouldHaveCauseInstanceof.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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful