How to use test method of io.kotest.matchers.string.matchers class

Best Kotest code snippet using io.kotest.matchers.string.matchers.test

AttachmentServiceTest.kt

Source:AttachmentServiceTest.kt Github

copy

Full Screen

...13import com.github.njuro.jard.embedData14import com.github.njuro.jard.metadata15import com.github.njuro.jard.multipartFile16import com.ninjasquad.springmockk.MockkBean17import io.kotest.matchers.booleans.shouldBeTrue18import io.kotest.matchers.file.shouldBeAFile19import io.kotest.matchers.file.shouldBeReadable20import io.kotest.matchers.file.shouldExist21import io.kotest.matchers.file.shouldHaveExtension22import io.kotest.matchers.file.shouldHaveNameWithoutExtension23import io.kotest.matchers.file.shouldNotBeEmpty24import io.kotest.matchers.file.shouldNotExist25import io.kotest.matchers.nulls.shouldBeNull26import io.kotest.matchers.nulls.shouldNotBeNull27import io.kotest.matchers.optional.shouldNotBePresent28import io.kotest.matchers.should29import io.kotest.matchers.shouldBe30import io.kotest.matchers.string.shouldNotBeBlank31import io.mockk.Runs32import io.mockk.every33import io.mockk.just34import org.junit.jupiter.api.AfterEach35import org.junit.jupiter.api.BeforeEach36import org.junit.jupiter.api.DisplayName37import org.junit.jupiter.api.Nested38import org.junit.jupiter.api.Test39import org.springframework.beans.factory.annotation.Autowired40import org.springframework.boot.test.context.SpringBootTest41import org.springframework.transaction.annotation.Transactional42import java.io.File43@SpringBootTest44@WithContainerDatabase45@Transactional46internal class AttachmentServiceTest {47 @Autowired48 private lateinit var attachmentService: AttachmentService49 @MockkBean50 private lateinit var remoteStorageService: RemoteStorageService51 @Autowired52 private lateinit var db: TestDataRepository53 @BeforeEach54 @AfterEach55 fun `delete test folder`() {56 val testFolder = attachmentPath(TEST_FOLDER_NAME).toFile()57 if (testFolder.exists()) {58 testFolder.deleteRecursively().shouldBeTrue()59 }60 }61 @Nested62 @DisplayName("save attachment")63 inner class SaveAttachment {64 private fun getRemoteUrl(folder: String, filename: String) = "https://remote-storage.com/$folder-$filename"65 @BeforeEach66 fun setUpMocks() {67 every {68 remoteStorageService.uploadFile(69 ofType(String::class),70 ofType(String::class),71 ofType(File::class)72 )...

Full Screen

Full Screen

FailingKotestAsserts.kt

Source:FailingKotestAsserts.kt Github

copy

Full Screen

1package testing.failing2import arrow.core.*3import io.kotest.assertions.arrow.either.shouldBeLeft4import io.kotest.assertions.arrow.either.shouldBeRight5import io.kotest.assertions.arrow.nel.shouldContain6import io.kotest.assertions.arrow.nel.shouldContainNull7import io.kotest.assertions.arrow.option.shouldBeNone8import io.kotest.assertions.arrow.option.shouldBeSome9import io.kotest.assertions.arrow.validation.shouldBeInvalid10import io.kotest.assertions.arrow.validation.shouldBeValid11import io.kotest.assertions.asClue12import io.kotest.assertions.assertSoftly13import io.kotest.assertions.json.*14import io.kotest.assertions.throwables.shouldThrowAny15import io.kotest.assertions.throwables.shouldThrowExactly16import io.kotest.assertions.withClue17import io.kotest.matchers.Matcher18import io.kotest.matchers.MatcherResult19import io.kotest.matchers.booleans.shouldBeFalse20import io.kotest.matchers.booleans.shouldBeTrue21import io.kotest.matchers.collections.shouldBeEmpty22import io.kotest.matchers.collections.shouldBeOneOf23import io.kotest.matchers.collections.shouldBeSameSizeAs24import io.kotest.matchers.collections.shouldBeSorted25import io.kotest.matchers.collections.shouldContain26import io.kotest.matchers.date.shouldBeAfter27import io.kotest.matchers.ints.shouldBeEven28import io.kotest.matchers.ints.shouldBeGreaterThan29import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual30import io.kotest.matchers.ints.shouldBeLessThan31import io.kotest.matchers.ints.shouldBeLessThanOrEqual32import io.kotest.matchers.ints.shouldBeZero33import io.kotest.matchers.maps.shouldBeEmpty34import io.kotest.matchers.maps.shouldContain35import io.kotest.matchers.maps.shouldContainKey36import io.kotest.matchers.maps.shouldContainValue37import io.kotest.matchers.nulls.shouldBeNull38import io.kotest.matchers.should39import io.kotest.matchers.shouldBe40import io.kotest.matchers.shouldNot41import io.kotest.matchers.string.shouldBeBlank42import io.kotest.matchers.string.shouldBeEmpty43import io.kotest.matchers.string.shouldBeUpperCase44import io.kotest.matchers.string.shouldContain45import io.kotest.matchers.string.shouldNotBeBlank46import java.time.LocalDate47import org.junit.jupiter.api.Test48/**49 * Kotest assertions50 *51 * - [Kotest Assertions Documentation](https://kotest.io/assertions/)52 * - [Github](https://github.com/kotest/kotest/)53 */54class FailingKotestAsserts {55 @Test56 fun `General assertions`() {57 assertSoftly {58 "text" shouldBe "txet"59 "hi".shouldBeBlank()60 " ".shouldNotBeBlank()61 "hi".shouldBeEmpty()62 "hi".shouldBeUpperCase()63 "hello".shouldContain("hi")64 false.shouldBeTrue()65 true.shouldBeFalse()66 "not null".shouldBeNull()67 10 shouldBeLessThan 1068 10 shouldBeLessThanOrEqual 969 11 shouldBeGreaterThan 1170 11 shouldBeGreaterThanOrEqual 1271 9.shouldBeEven()72 1.shouldBeZero()73 }74 }75 @Test76 fun `Exception assertions`() {77 assertSoftly {78 shouldThrowExactly<IllegalArgumentException> {79 angryFunction()80 }81 shouldThrowAny {82 "I'm not throwing anything"83 }84 }85 }86 @Test87 fun `Collection assertions`() {88 assertSoftly {89 listOf(1, 2, 3).shouldBeEmpty()90 listOf(1, 2, 3) shouldContain 491 listOf(1, 3, 2).shouldBeSorted()92 listOf(1, 2, 3, 4) shouldBeSameSizeAs listOf(4, 5, 6)93 1 shouldBeOneOf listOf(2, 3)94 mapOf(1 to "one", 2 to "two", 3 to "three").shouldBeEmpty()95 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainKey 496 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainValue "five"97 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContain (6 to "six")98 }99 }100 @Test101 fun `Arrow assertions`() {102 assertSoftly {103 val optionNone = none<String>()104 val optionSome = Some("I am something").toOption()105 optionSome.shouldBeNone()106 optionNone.shouldBeSome()107 val rightEither = Either.Right(1)108 val leftEither = Either.Left("ERROR!!")109 leftEither.shouldBeRight()110 leftEither shouldBeRight 1111 rightEither.shouldBeLeft()112 rightEither shouldBeLeft "ERROR!!"113 val nonEmptyList = NonEmptyList.of(1, 2, 3, 4, 5)114 nonEmptyList shouldContain 6115 nonEmptyList.shouldContainNull()116 val valid = Validated.valid()117 val invalid = Validated.invalid()118 invalid.shouldBeValid()119 valid.shouldBeInvalid()120 }121 }122 @Test123 fun `Json assertions`() {124 val jsonString = "{\"test\": \"property\", \"isTest\": true }"125 assertSoftly {126 jsonString shouldMatchJson "{\"test\": \"otherProperty\"}"127 jsonString shouldContainJsonKey "$.anotherTest"128 jsonString shouldNotContainJsonKey "$.test"129 jsonString.shouldContainJsonKeyValue("$.isTest", false)130 }131 }132 @Test133 fun `Custom assertions`() {134 assertSoftly {135 val sentMail = Mail(136 dateCreated = LocalDate.of(2020, 10, 27),137 sent = true, message = "May you have an amazing day"138 )139 val unsentMail = Mail(140 dateCreated = LocalDate.of(2020, 10, 27),141 sent = false, message = "May you have an amazing day"142 )143 // This is possible144 unsentMail.sent should beSent()145 sentMail.sent shouldNot beSent()146 // This is recommended147 unsentMail.shouldBeSent()148 sentMail.shouldNotBeSent()149 }150 }151 @Test152 fun `withClue usage`() {153 val mail = Mail(154 dateCreated = LocalDate.of(2020, 10, 27),155 sent = false, message = "May you have an amazing day"156 )157 withClue("sent field should be true") {158 mail.sent shouldBe true159 }160 mail.asClue {161 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)162 it.sent shouldBe true163 }164 }165 @Test166 fun `asClue usage`() {167 val mail = Mail(168 dateCreated = LocalDate.of(2020, 10, 27),169 sent = false, message = "May you have an amazing day"170 )171 mail.asClue {172 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)173 it.sent shouldBe true174 }175 }176 fun beSent() = object : Matcher<Boolean> {177 override fun test(value: Boolean) = MatcherResult(value, "Mail.sent should be true", "Mail.sent should be false")178 }179 fun Mail.shouldBeSent() = this.sent should beSent()180 fun Mail.shouldNotBeSent() = this.sent shouldNot beSent()181}182data class Mail(val dateCreated: LocalDate, val sent: Boolean, val message: String)183fun angryFunction() {184 throw IllegalStateException("How dare you!")185}...

Full Screen

Full Screen

KotestAsserts.kt

Source:KotestAsserts.kt Github

copy

Full Screen

1package testing.asserts2import arrow.core.*3import io.kotest.assertions.arrow.either.shouldBeLeft4import io.kotest.assertions.arrow.either.shouldBeRight5import io.kotest.assertions.arrow.nel.shouldContain6import io.kotest.assertions.arrow.nel.shouldContainNull7import io.kotest.assertions.arrow.option.shouldBeNone8import io.kotest.assertions.arrow.option.shouldBeSome9import io.kotest.assertions.arrow.validation.shouldBeInvalid10import io.kotest.assertions.arrow.validation.shouldBeValid11import io.kotest.assertions.asClue12import io.kotest.assertions.json.*13import io.kotest.assertions.throwables.shouldThrowAny14import io.kotest.assertions.throwables.shouldThrowExactly15import io.kotest.assertions.withClue16import io.kotest.matchers.Matcher17import io.kotest.matchers.MatcherResult18import io.kotest.matchers.booleans.shouldBeFalse19import io.kotest.matchers.booleans.shouldBeTrue20import io.kotest.matchers.collections.shouldBeEmpty21import io.kotest.matchers.collections.shouldBeOneOf22import io.kotest.matchers.collections.shouldBeSameSizeAs23import io.kotest.matchers.collections.shouldBeSorted24import io.kotest.matchers.collections.shouldContain25import io.kotest.matchers.date.shouldBeAfter26import io.kotest.matchers.ints.shouldBeEven27import io.kotest.matchers.ints.shouldBeGreaterThan28import io.kotest.matchers.ints.shouldBeGreaterThanOrEqual29import io.kotest.matchers.ints.shouldBeLessThan30import io.kotest.matchers.ints.shouldBeLessThanOrEqual31import io.kotest.matchers.ints.shouldBeZero32import io.kotest.matchers.maps.shouldBeEmpty33import io.kotest.matchers.maps.shouldContain34import io.kotest.matchers.maps.shouldContainKey35import io.kotest.matchers.maps.shouldContainValue36import io.kotest.matchers.nulls.shouldBeNull37import io.kotest.matchers.should38import io.kotest.matchers.shouldBe39import io.kotest.matchers.shouldNot40import io.kotest.matchers.string.shouldBeBlank41import io.kotest.matchers.string.shouldBeEmpty42import io.kotest.matchers.string.shouldBeUpperCase43import io.kotest.matchers.string.shouldContain44import io.kotest.matchers.string.shouldNotBeBlank45import java.time.LocalDate46import org.junit.jupiter.api.Test47/**48 * Kotest assertions49 *50 * - [Kotest Assertions Documentation](https://kotest.io/assertions/)51 * - [Github](https://github.com/kotest/kotest/)52 */53class KotestAsserts {54 @Test55 fun `General assertions`() {56 "text" shouldBe "text"57 " ".shouldBeBlank()58 "hi".shouldNotBeBlank()59 "".shouldBeEmpty()60 "HI".shouldBeUpperCase()61 "hello".shouldContain("ll")62 true.shouldBeTrue()63 false.shouldBeFalse()64 null.shouldBeNull()65 10 shouldBeLessThan 1166 10 shouldBeLessThanOrEqual 1067 11 shouldBeGreaterThan 1068 11 shouldBeGreaterThanOrEqual 1169 10.shouldBeEven()70 0.shouldBeZero()71 }72 @Test73 fun `Exception assertions`() {74 shouldThrowExactly<IllegalStateException> {75 angryFunction()76 }77 shouldThrowAny {78 angryFunction()79 }80 }81 @Test82 fun `Collection assertions`() {83 emptyList<Int>().shouldBeEmpty()84 listOf(1, 2, 3) shouldContain 385 listOf(1, 2, 3).shouldBeSorted()86 listOf(1, 2, 3) shouldBeSameSizeAs listOf(4, 5, 6)87 1 shouldBeOneOf listOf(1, 2, 3)88 emptyMap<Int, String>().shouldBeEmpty()89 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainKey 190 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContainValue "two"91 mapOf(1 to "one", 2 to "two", 3 to "three") shouldContain (3 to "three")92 }93 @Test94 fun `Arrow assertions`() {95 val optionNone = none<String>()96 val optionSome = Some("I am something").toOption()97 optionNone.shouldBeNone()98 optionSome.shouldBeSome()99 val rightEither = Either.Right(1)100 val leftEither = Either.Left("ERROR!!")101 rightEither.shouldBeRight()102 rightEither shouldBeRight 1103 leftEither.shouldBeLeft()104 leftEither shouldBeLeft "ERROR!!"105 val nonEmptyList = NonEmptyList.of(1, 2, 3, 4, 5, null)106 nonEmptyList shouldContain 1107 nonEmptyList.shouldContainNull()108 val valid = Validated.valid()109 val invalid = Validated.invalid()110 valid.shouldBeValid()111 invalid.shouldBeInvalid()112 }113 @Test114 fun `Json assertions`() {115 val jsonString = "{\"test\": \"property\", \"isTest\": true }"116 jsonString shouldMatchJson jsonString117 jsonString shouldContainJsonKey "$.test"118 jsonString shouldNotContainJsonKey "$.notTest"119 jsonString.shouldContainJsonKeyValue("$.isTest", true)120 }121 @Test122 fun `Custom assertions`() {123 val sentMail = Mail(124 dateCreated = LocalDate.of(2020, 10, 27),125 sent = true, message = "May you have an amazing day"126 )127 val unsentMail = Mail(128 dateCreated = LocalDate.of(2020, 10, 27),129 sent = false, message = "May you have an amazing day"130 )131 // This is possible132 sentMail.sent should beSent()133 unsentMail.sent shouldNot beSent()134 // This is recommended135 sentMail.shouldBeSent()136 unsentMail.shouldNotBeSent()137 }138 @Test139 fun `withClue usage`() {140 val mail = Mail(141 dateCreated = LocalDate.of(2020, 10, 27),142 sent = false, message = "May you have an amazing day"143 )144 withClue("sent field should be false") {145 mail.sent shouldBe false146 }147 mail.asClue {148 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)149 it.sent shouldBe false150 }151 }152 @Test153 fun `asClue usage`() {154 val mail = Mail(155 dateCreated = LocalDate.of(2020, 10, 27),156 sent = false, message = "May you have an amazing day"157 )158 mail.asClue {159 it.dateCreated shouldBeAfter LocalDate.of(2020, 10, 26)160 it.sent shouldBe false161 }162 }163 fun beSent() = object : Matcher<Boolean> {164 override fun test(value: Boolean) = MatcherResult(value, "Mail.sent should be true", "Mail.sent should be false")165 }166 fun Mail.shouldBeSent() = this.sent should beSent()167 fun Mail.shouldNotBeSent() = this.sent shouldNot beSent()168}169data class Mail(val dateCreated: LocalDate, val sent: Boolean, val message: String)170fun angryFunction() {171 throw IllegalStateException("How dare you!")172}...

Full Screen

Full Screen

InMemoryHopRepositoryTest.kt

Source:InMemoryHopRepositoryTest.kt Github

copy

Full Screen

...4import domain.hop.model.HopType5import domain.quantities.PercentRange6import domain.quantities.QuantityRange7import fixtures.sampleHop8import io.kotest.assertions.assertSoftly9import io.kotest.assertions.fail10import io.kotest.core.spec.style.ShouldSpec11import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder12import io.kotest.matchers.maps.shouldHaveSize13import io.kotest.matchers.nulls.shouldNotBeNull14import io.kotest.matchers.shouldBe15import io.kotest.matchers.string.shouldNotBeBlank16class InMemoryHopRepositoryTest : ShouldSpec({17 val repoUnderTest = InMemoryHopRepository()18 beforeEach {19 repoUnderTest.clear()20 }21 should("find hop by name") {22 // Givent23 repoUnderTest.save(sampleHop)24 repoUnderTest.save(sampleHop.copy(name = "Cascade", similarTo = listOf("Citra")))25 repoUnderTest.save(sampleHop.copy(name = "Chinook", similarTo = listOf("Cascade", "Citra", "Chinook")))26 // When & Then27 repoUnderTest.findByName("Citra").shouldNotBeNull().apply {28 assertSoftly {29 name shouldBe "Citra"...

Full Screen

Full Screen

CategoryRepositorySelectSpec.kt

Source:CategoryRepositorySelectSpec.kt Github

copy

Full Screen

1package com.gaveship.category.domain.model2import com.gaveship.category.Mock3import io.kotest.core.spec.style.StringSpec4import io.kotest.matchers.collections.shouldHaveSize5import io.kotest.matchers.collections.singleElement6import io.kotest.matchers.nulls.beNull7import io.kotest.matchers.shouldBe8import io.kotest.matchers.shouldHave9import io.kotest.matchers.shouldNot10import io.kotest.property.arbitrary.chunked11import io.kotest.property.arbitrary.single12import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest13import org.springframework.data.jpa.repository.config.EnableJpaAuditing14@EnableJpaAuditing15@DataJpaTest(16 showSql = true,17 properties = [18 "spring.flyway.enabled=false",19 "spring.jpa.hibernate.ddl-auto=create"20 ]21)22class CategoryRepositorySelectSpec(23 private val categoryRepository: CategoryRepository24) : StringSpec() {25 init {26 "Root Categories 조회 성공 Test" {...

Full Screen

Full Screen

KoTestTest.kt

Source:KoTestTest.kt Github

copy

Full Screen

1package com.example.testingcomparison2import io.kotest.assertions.assertSoftly3import io.kotest.matchers.Matcher4import io.kotest.matchers.MatcherResult5import io.kotest.matchers.collections.shouldContain6import io.kotest.matchers.collections.shouldHaveSize7import io.kotest.matchers.collections.shouldNotContain8import io.kotest.matchers.should9import io.kotest.matchers.types.shouldBeInstanceOf10import org.junit.Test11class KoTestTest {12 private val animals = listOf("Rex", "Caramel", "Joe", "Anna")13 @Test14 fun kotest() {15 animals should containPalyndrom()16 animals17 .shouldBeInstanceOf<List<String>>()18 .shouldContain("Rex") // Doesn't let me chain here19 animals.shouldNotContain("Snow") // Doesn't let me chain here20 animals.shouldHaveSize(3)21 }22 @Test23 fun kotestSoftList() {24 assertSoftly {25 animals.shouldHaveSize(2)26 animals27 .shouldBeInstanceOf<List<String>>()28 .shouldContain("Rex") // Doesn't let me chain here29 animals.shouldNotContain("Snow") // Doesn't let me chain here30 animals.shouldHaveSize(3)31 }32 }33 fun containPalyndrom() = object : Matcher<List<String>> {34 override fun test(value: List<String>): MatcherResult {35 return MatcherResult(36 value.any { it.reversed().equals(it, ignoreCase = true) },37 "List should contain palindrome",38 "List shouldn't contain palindrome"39 )40 }41 }42}...

Full Screen

Full Screen

FunSpecSimpleTest.kt

Source:FunSpecSimpleTest.kt Github

copy

Full Screen

1package com.kotlinspring.styles2import io.kotest.assertions.json.shouldEqualJson3import io.kotest.assertions.json.shouldNotEqualJson4import io.kotest.core.spec.style.FunSpec5import io.kotest.matchers.should6import io.kotest.matchers.shouldBe7import io.kotest.matchers.string.endWith8import io.kotest.matchers.string.shouldContain9import io.kotest.matchers.string.startWith10class FunSpecSimpleTest : FunSpec({11 test("name of tester should return the correct length") {12 val nameTester = "Matheus Marin"13 nameTester.shouldContain("Matheus")14 nameTester.length shouldBe 1315 nameTester should startWith("Matheus")16 nameTester should endWith("Marin")17 }18 test("a json with a developer should be valid") {19 val json = """ { "age" : 23, "name": "matheus", "location": "sao paulo" } """20 json.shouldEqualJson(returnJsonOfAValidDev())21 }22 test("a json with a PO should be invalid") {23 val json = """ { "age" : 45, "name": "robert", "location": "rio de janeiro" } """24 json.shouldNotEqualJson(returnJsonOfAValidDev())25 }26}) {27 companion object {28 fun returnJsonOfAValidDev() : String{29 return """ { "age" : 23, "name": "matheus", "location": "sao paulo" } """30 }31 }32}...

Full Screen

Full Screen

MyTest.kt

Source:MyTest.kt Github

copy

Full Screen

1package ch.keepcalm.demo.kofu2import io.github.serpro69.kfaker.Faker3import io.kotest.core.spec.style.StringSpec4import io.kotest.matchers.comparables.shouldNotBeLessThan5import io.kotest.matchers.should6import io.kotest.matchers.shouldBe7import io.kotest.matchers.string.startWith8class MyTest : StringSpec({9 "length should return size of string" {10 "hello".length shouldBe 511 }12 "startsWith should test for a prefix" {13 "world" should startWith("wor")14 }15 "should give back any first name with less than 1 character's" {16 Faker().name.firstName() shouldNotBeLessThan "1"17 }18})...

Full Screen

Full Screen

test

Using AI Code Generation

copy

Full Screen

1stringMatcher(“Hello”, “Hello”)2stringMatcher(“Hello”, “Hello”)3stringMatcher(“Hello”, “Hello”)4stringMatcher(“Hello”, “Hello”)5stringMatcher(“Hello”, “Hello”)6stringMatcher(“Hello”, “Hello”)7stringMatcher(“Hello”, “Hello”)8stringMatcher(“Hello”, “Hello”)9stringMatcher(“Hello”, “Hello”)10stringMatcher(“Hello”, “Hello”)11stringMatcher(“Hello”, “Hello”)

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