Best Kotest code snippet using io.kotest.matchers.maps.matchers.Map.shouldNotContainValues
MapMatchersTest.kt
Source:MapMatchersTest.kt  
1package com.sksamuel.kotest.matchers.maps2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.WordSpec4import io.kotest.matchers.and5import io.kotest.matchers.maps.*6import io.kotest.matchers.should7import io.kotest.matchers.shouldBe8import io.kotest.matchers.shouldNot9import java.util.LinkedList10class MapMatchersTest : WordSpec() {11   init {12      "haveKey" should {13         "test that a map contains the given key" {14            val map = mapOf(Pair(1, "a"), Pair(2, "b"))15            map should haveKey(1)16            map.shouldContainKey(1)17            map.shouldNotContainKey(4)18            shouldThrow<AssertionError> {19               map should haveKey(3)20            }21            shouldThrow<AssertionError> {22               map.shouldContainKey(5)23            }.message.shouldBe("Map should contain key 5")24            shouldThrow<AssertionError> {25               map.shouldNotContainKey(1)26            }.message.shouldBe("Map should not contain key 1")27         }28         "support maps and mutable maps" {29            val mutable = mutableMapOf("a" to "b")30            mutable.shouldHaveKey("a")31            val map = mapOf("a" to "b")32            map.shouldHaveKey("a")33         }34      }35      "haveValue" should {36         "test that a map contains the given value" {37            val map = mapOf(Pair(1, "a"), Pair(2, "b"))38            map should haveValue("a")39            map.shouldContainValue("a")40            map.shouldNotContainValue("A")41            shouldThrow<AssertionError> {42               map should haveValue("c")43            }44            shouldThrow<AssertionError> {45               map.shouldContainValue("c")46            }.message.shouldBe("Map should contain value c")47         }48      }49      "contain" should {50         "test that a map contains the given pair" {51            val map = mapOf(Pair(1, "a"), Pair(2, "b"))52            map should contain(1, "a")53            map.shouldContain(2, "b")54            map.shouldNotContain(3, "A")55            map shouldContain (1 to "a")56            map shouldNotContain (3 to "A")57            shouldThrow<AssertionError> {58               map.shouldContain(1, "c")59            }.message.shouldBe("Map should contain mapping 1=c but was 1=a")60            shouldThrow<AssertionError> {61               map.shouldContain(4, "e")62            }.message.shouldBe("Map should contain mapping 4=e but was {1=a, 2=b}")63            shouldThrow<AssertionError> {64               map should contain(2, "a")65            }.message.shouldBe("Map should contain mapping 2=a but was 2=b")66         }67      }68      "haveKeys" should {69         "test that a map contains all given keys" {70            val map = mapOf(Pair(1, "a"), Pair(2, "b"), Pair(3, "c"))71            map should haveKeys(1, 3)72            map should haveKeys(1, 2, 3)73            map.shouldContainKeys(1, 3)74            map.shouldContainKeys(1, 2, 3)75            shouldThrow<AssertionError> {76               map should haveKeys(4)77            }78            shouldThrow<AssertionError> {79               map should haveKeys(1, 4)80            }81            shouldThrow<AssertionError> {82               map.shouldContainKeys(1, 4, 5, 6)83            }.message.shouldBe("Map did not contain the keys 4, 5, 6")84         }85      }86      "haveValues" should {87         "test that a map contains all given values" {88            val map = mapOf(Pair(1, "a"), Pair(2, "b"), Pair(3, "c"))89            map should haveValues("b", "c")90            map should haveValues("a", "b", "c")91            map.shouldContainValues("b", "c")92            map.shouldContainValues("a", "b", "c")93            shouldThrow<AssertionError> {94               map should haveValues("a", "d")95            }96            shouldThrow<AssertionError> {97               map should haveValues("d")98            }99            shouldThrow<AssertionError> {100               map.shouldContainValues("d")101            }.message.shouldBe("Map did not contain the values d")102            shouldThrow<AssertionError> {103               map.shouldNotContainValues("a", "b")104            }.message.shouldBe("Map should not contain the values a, b")105         }106      }107      "containAnyKeys" should {108         "test that a map contains any of the given keys"{109            val map = mapOf("a" to 1, "b" to 2, "c" to 3)110            map should containAnyKeys("a", "x", "y")111            map should containAnyKeys("a", "b", "c")112            map should containAnyKeys("a", "b")113            map.shouldContainAnyKeysOf("a", "c")114            shouldThrow<AssertionError> {115               map should containAnyKeys("x", "y", "z")116            }117            shouldThrow<AssertionError> {118               map.shouldContainAnyKeysOf("x", "y")119            }120            shouldThrow<AssertionError> {121               map.shouldNotContainAnyKeysOf("a", "y")122            }123         }124      }125      "containAnyValues" should {126         "test that a map contains any of the given values"{127            val map = mapOf("a" to 1, "b" to 2, "c" to 3)128            map should containAnyValues(1, 23, 24)129            map should containAnyValues(1, 2, 3)130            map should containAnyValues(3, 2)131            map.shouldContainAnyValuesOf(2, 3)132            shouldThrow<AssertionError> {133               map should containAnyValues(9, 8, 7)134            }135            shouldThrow<AssertionError> {136               map.shouldContainAnyValuesOf(4, 5)137            }138            shouldThrow<AssertionError> {139               map.shouldNotContainAnyValuesOf(1,5)140            }141         }142      }143      "containAll" should {144         "test that a map contains all given pairs" {145            val map = mapOf(Pair(1, "a"), Pair(2, "b"), Pair(3, "c"))146            map should containAll(mapOf(1 to "a", 3 to "c"))147            map should containAll(mapOf(3 to "c"))148            map.shouldContainAll(mapOf(1 to "a", 3 to "c"))149            map.shouldNotContainAll(mapOf(1 to "a", 3 to "h"))150         }151         "test empty map" {152            emptyMap<Any, Any>() should containAll(emptyMap<Any, Any>())153            emptyMap<Any, Any>().shouldContainAll(emptyMap<Any, Any>())154         }155         "test assertion that map does not contain entries from the given map" {156            val e = shouldThrow<AssertionError> {157               emptyMap<Any, Any>() should containAll(mapOf<Any, Any>("\$a" to 1))158            }159            e.message shouldBe """160          |161          |Expected:162          |  mapOf()163          |should contain all of:164          |  mapOf("\${'$'}a" to 1)165          |but differs by:166          |  missing keys:167          |    "\${'$'}a"168          |169        """.trimMargin()170         }171         "test when map contains extra entries" {172            mapOf("a" to 1, "b" to 2) should (173               containAll(mapOf("a" to 1)) and containAll(mapOf("b" to 2)))174         }175         "test assertion when map contains different value type" {176            val e = shouldThrow<AssertionError> {177               mapOf("a" to 1) should containAll(mapOf<String, Any>("a" to 1L))178            }179            e.message shouldBe """180          |181          |Expected:182          |  mapOf("a" to 1)183          |should contain all of:184          |  mapOf("a" to 1L)185          |but differs by:186          |  different values:187          |    "a":188          |      expected:189          |        1L190          |      but was:191          |        1192          |193        """.trimMargin()194         }195         "test that a map with nested map contains all entries from the given map" {196            val map = mapOf("a" to mapOf("b" to 2))197            map should containAll(mapOf("a" to mapOf("b" to 2)))198            val e = shouldThrow<AssertionError> {199               map should containAll(mapOf("a" to mapOf("b" to 3)))200            }201            e.message shouldBe """202          |203          |Expected:204          |  mapOf("a" to mapOf("b" to 2))205          |should contain all of:206          |  mapOf("a" to mapOf("b" to 3))207          |but differs by:208          |  different values:209          |    "a":210          |      different values:211          |        "b":212          |          expected:213          |            3214          |          but was:215          |            2216          |217        """.trimMargin()218         }219         "test shouldNot assertion" {220            val e = shouldThrow<AssertionError> {221               mapOf("a" to 1, "b" to 2) shouldNot containAll(mapOf("a" to 1))222            }223            e.message shouldBe """224          |225          |Expected:226          |  mapOf("a" to 1, "b" to 2)227          |should not contain all of:228          |  mapOf("a" to 1)229          |but contains230          |231        """.trimMargin()232         }233      }234      "containExactly" should {235         "test empty map" {236            emptyMap<Any, Any>() should containExactly(emptyMap<Any, Any>())237            emptyMap<Any, Any>().shouldContainExactly(emptyMap<Any, Any>())238         }239         "test assertion that a map contains extra keys" {240            val e = shouldThrow<AssertionError> {241               mapOf("a" to 1) should containExactly(emptyMap<String, Any>())242            }243            e.message shouldBe """244          |245          |Expected:246          |  mapOf("a" to 1)247          |should be equal to:248          |  mapOf()249          |but differs by:250          |  extra keys:251          |    "a"252          |253        """.trimMargin()254         }255         "test shouldNot assertion" {256            val e = shouldThrow<AssertionError> {257               val arrayList: List<Int> = arrayListOf(1)258               val linkedList = LinkedList<Int>()259               linkedList.push(1)260               mapOf("a" to arrayList) shouldNot containExactly<String, List<Int>>(mapOf("a" to linkedList))261               mapOf("a" to arrayList) shouldNot containExactly<String, List<Int>>("a" to linkedList)262            }263            e.message shouldBe """264          |265          |Expected:266          |  mapOf("a" to listOf(1))267          |should not be equal to:268          |  mapOf("a" to listOf(1))269          |but equals270          |271        """.trimMargin()272         }273      }274      "be empty" should {275         "Work on an empty map" {276            emptyMap<String, String>().shouldBeEmpty()277         }278         "Fail on a non empty map" {279            shouldThrow<AssertionError> {280               mapOf("Foo" to "Bar").shouldBeEmpty()281            }282         }283      }284      "Not be empty" should {285         "Fail on an empty map" {286            shouldThrow<AssertionError> {287               emptyMap<String, String>().shouldNotBeEmpty()288            }289         }290         "Pass on a non empty map" {291            mapOf("Foo" to "Bar").shouldNotBeEmpty()292         }293      }294      "haveSize" should {295         "test that a map has given size" {296            val map = mapOf(1 to "a", 2 to "b")297            map should haveSize(2)298            map shouldHaveSize (2)299            shouldThrow<AssertionError> {300               map should haveSize(3)301            }302            shouldThrow<AssertionError> {303               map shouldHaveSize (3)304            }305         }306      }307      "map comparision" should {308         "give formatted error in case of failure assertion error with shouldBeEqualTo" {309            val map1 = mapOf(310               "Key1" to "Val11",311               "Key2" to "Val21",312               "Key3" to "Val31",313               "Key4" to "Val41",314               "Key5" to "Val51",315               "Key6" to "Val61",316               "Key7" to "Val71",317               "Key8" to "Val81",318               "Key9" to "Val91",319               "Key10" to "Val101",320               "Key11" to "Val111",321               "Key12" to "Val121"322            )323            val map2 = mapOf(324               "Key1" to "Val11",325               "Key2" to "Val22",326               "Key3" to "Val32",327               "Key4" to "Val42",328               "Key5" to "Val52",329               "Key6" to "Val62",330               "Key7" to "Val72",331               "Key8" to "Val82",332               "Key9" to "Val92",333               "Key10" to "Val102",334               "Key11" to "Val112",335               "Key12" to "Val122"336            )337            val expectedErrorMessage = """Expected338{339  "Key1" = "Val11",340  "Key2" = "Val22",341  "Key3" = "Val32",342  "Key4" = "Val42",343  "Key5" = "Val52",344  "Key6" = "Val62",345  "Key7" = "Val72",346  "Key8" = "Val82",347  "Key9" = "Val92",348  "Key10" = "Val102",349...350}351to be equal to352{353  "Key1" = "Val11",354  "Key2" = "Val21",355  "Key3" = "Val31",356  "Key4" = "Val41",357  "Key5" = "Val51",358  "Key6" = "Val61",359  "Key7" = "Val71",360  "Key8" = "Val81",361  "Key9" = "Val91",362  "Key10" = "Val101",363...364}365Values differed at keys Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Key10, Key11, ...""".trimMargin()366            val assertionError = shouldThrow<AssertionError> { map1 shouldBe map2 }367            assertionError.message shouldBe """Values differed at keys Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Key10, Key11, ...368expected:<{369  "Key1" = "Val11",370  "Key2" = "Val22",371  "Key3" = "Val32",372  "Key4" = "Val42",373  "Key5" = "Val52",374  "Key6" = "Val62",375  "Key7" = "Val72",376  "Key8" = "Val82",377  "Key9" = "Val92",378  "Key10" = "Val102",379...380}> but was:<{381  "Key1" = "Val11",382  "Key2" = "Val21",383  "Key3" = "Val31",384  "Key4" = "Val41",385  "Key5" = "Val51",386  "Key6" = "Val61",387  "Key7" = "Val71",388  "Key8" = "Val81",389  "Key9" = "Val91",390  "Key10" = "Val101",391...392}>"""393         }394      }395   }396}...matchers.kt
Source:matchers.kt  
1package io.kotest.matchers.maps2import io.kotest.matchers.Matcher3import io.kotest.matchers.MatcherResult4import io.kotest.matchers.should5import io.kotest.matchers.shouldNot6fun <K, V> mapcontain(key: K, v: V) = object : Matcher<Map<K, V>> {7  override fun test(value: Map<K, V>) = MatcherResult(8     value[key] == v,9     "Map should contain mapping $key=$v but was ${buildActualValue(value)}",10     "Map should not contain mapping $key=$v but was $value"11  )12  private fun buildActualValue(map: Map<K, V>) = map[key]?.let { "$key=$it" } ?: map13}14fun <K, V> Map<K, V>.shouldContain(key: K, value: V) = this should mapcontain(key, value)15fun <K, V> Map<K, V>.shouldNotContain(key: K, value: V) = this shouldNot mapcontain(key, value)16infix fun <K, V> Map<K, V>.shouldContain(entry: Pair<K, V>) =17  this should mapcontain(entry.first, entry.second)18infix fun <K, V> Map<K, V>.shouldNotContain(entry: Pair<K, V>) =19  this shouldNot mapcontain(entry.first, entry.second)20infix fun <K, V> Map<K, V>.shouldContainExactly(expected: Map<K, V>) =21  this should containExactly(expected)22infix fun <K, V> Map<K, V>.shouldNotContainExactly(expected: Map<K, V>) =23  this shouldNot containExactly(expected)24infix fun <K, V> Map<K, V>.shouldContainAll(expected: Map<K, V>) = this should containAll(expected)25infix fun <K, V> Map<K, V>.shouldNotContainAll(expected: Map<K, V>) =26  this shouldNot containAll(expected)27infix fun <K, V : Any> Map<K, V>.shouldHaveKey(key: K) = this should haveKey(key)28infix fun <K, V : Any> Map<K, V>.shouldContainKey(key: K) = this should haveKey(key)29infix fun <K, V : Any> Map<K, V>.shouldNotHaveKey(key: K) = this shouldNot haveKey(key)30infix fun <K, V : Any> Map<K, V>.shouldNotContainKey(key: K) = this shouldNot haveKey(key)31infix fun <K, V> Map<K, V>.shouldContainValue(value: V) = this should haveValue<V>(value)32infix fun <K, V> Map<K, V>.shouldNotContainValue(value: V) = this shouldNot haveValue<V>(value)33infix fun <K, V> Map<K, V>.shouldHaveSize(size: Int) = this should haveSize(size)34fun <K, V> Map<K, V>.shouldHaveKeys(vararg keys: K) = this should haveKeys(*keys)35fun <K, V> Map<K, V>.shouldContainKeys(vararg keys: K) = this should haveKeys(*keys)36fun <K, V> Map<K, V>.shouldNotHaveKeys(vararg keys: K) = this shouldNot haveKeys(*keys)37fun <K, V> Map<K, V>.shouldNotContainKeys(vararg keys: K) = this shouldNot haveKeys(*keys)38fun <K, V> Map<K, V>.shouldHaveValues(vararg values: V) = this should haveValues(*values)39fun <K, V> Map<K, V>.shouldContainValues(vararg values: V) = this should haveValues(*values)40fun <K, V> Map<K, V>.shouldNotHaveValues(vararg values: V) = this shouldNot haveValues(*values)41fun <K, V> Map<K, V>.shouldNotContainValues(vararg values: V) = this shouldNot haveValues(*values)42fun <K, V> Map<K, V>.shouldBeEmpty() = this should beEmpty()43fun <K, V> Map<K, V>.shouldNotBeEmpty() = this shouldNot beEmpty()44fun beEmpty() = object : Matcher<Map<*, *>> {45  override fun test(value: Map<*, *>): MatcherResult {46    return MatcherResult(47       value.isEmpty(),48       { "Map should be empty, but was $value." },49       { "Map should not be empty, but was." }50    )51  }52}...Map.shouldNotContainValues
Using AI Code Generation
1val map = mapOf("a" to 1, "b" to 2, "c" to 3) map.shouldNotContainValues(4, 5, 6) map.shouldNotContainValues(1, 2, 3) map.shouldNotContainValues(1, 2) map.shouldNotContainValues(1) map.shouldNotContainValues(2) map.shouldNotContainValues(3)2val map = mapOf("a" to 1, "b" to 2, "c" to 3) map.shouldNotContainKeys("d", "e", "f") map.shouldNotContainKeys("a", "b", "c") map.shouldNotContainKeys("a", "b") map.shouldNotContainKeys("a") map.shouldNotContainKeys("b") map.shouldNotContainKeys("c")3val map = mapOf("a" to 1, "b" to 2, "c" to 3) map.shouldContainKey("a") map.shouldContainKey("b") map.shouldContainKey("c") map.shouldContainKey("d")4val map = mapOf("a" to 1, "b" to 2, "c" to 3) map.shouldContainValue(1) map.shouldContainValue(2) map.shouldContainValue(3) map.shouldContainValue(4)5val map = mapOf<String, Int>() map.shouldBeEmpty()6val map = mapOf("a" to 1, "b" to 2, "c" to 3) map.shouldNotBeEmpty()7val map = mapOf("a" to 1, "b" to 2, "c" to 3) map.shouldHaveSize(3) map.shouldHaveSize(Map.shouldNotContainValues
Using AI Code Generation
1val map = mapOf(1 to "one", 2 to "two", 3 to "three")2map.shouldNotContainValues("four", "five")3val map = mapOf(1 to "one", 2 to "two", 3 to "three")4map.shouldContainKey(1)5val map = mapOf(1 to "one", 2 to "two", 3 to "three")6map.shouldContainKeys(1, 2)7val map = mapOf(1 to "one", 2 to "two", 3 to "three")8map.shouldNotContainKey(4)9val map = mapOf(1 to "one", 2 to "two", 3 to "three")10map.shouldNotContainKeys(4, 5)11val map = mapOf(1 to "one", 2 to "two", 3 to "three")12map.shouldContainAll(listOf(1 to "one", 2 to "two"))13val map = mapOf(1 to "one", 2 to "two", 3 to "three")14map.shouldContainNone(listOf(4 to "four", 5 to "five"))15val map = mapOf(1 to "one", 2 to "two", 3 to "three")16map.shouldBeEmpty()17val map = mapOf(1 to "one", 2 to "two", 3 to "three")18map.shouldNotBeEmpty()Map.shouldNotContainValues
Using AI Code Generation
1val map = mapOf( "a" to 1 , "b" to 2 , "c" to 3 , "d" to 4 , "e" to 5 ) map shouldNotContainValues 2 , 42val map = mapOf( "a" to 1 , "b" to 2 , "c" to 3 , "d" to 4 , "e" to 5 ) map shouldNotContainNulls3val map = mapOf( "a" to 1 , "b" to 2 , "c" to 3 , "d" to 4 , "e" to 5 ) map shouldNotContainKeys "a" , "b"4val map = mapOf( "a" to 1 , "b" to 2 , "c" to 3 , "d" to 4 , "e" to 5 ) map shouldNotContainKey "a"5val map = mapOf( "a" to 1 , "b" to 2 , "c" to 3 , "d" to 4 , "e" to 5 ) map shouldNotContainValue 26val map = mapOf( "a" to 1 , "b" to 2 , "c" to 3 , "d" to 4 , "e" to 5 ) map shouldNotBeEmpty7val map = mapOf( "a" to 1 , "b" to 2 , "c" to 3 , "d" to 4 , "e" to 5 ) map shouldNotBeEmptyMap.shouldNotContainValues
Using AI Code Generation
1val map = mapOf("a" to 1, "b" to 2, "c" to 3)2map.shouldNotContainValues(4, 5)3val map = mapOf("a" to 1, "b" to 2, "c" to 3)4map.shouldNotContainNulls()5val map = mapOf("a" to 1, "b" to 2, "c" to 3)6map.shouldNotBeEmpty()7val map = mapOf("a" to 1, "b" to 2, "c" to 3)8map.shouldBeEmpty()9val map = mapOf("a" to 1, "b" to 2, "c" to 3)10map.shouldHaveSize(3)11val map = mapOf("a" to 1, "b" to 2, "c" to 3)12map.shouldBeSingleton()13val map = mapOf("a" to 1, "b" to 2, "c" to 3)14map.shouldContainKeys("a", "b")15val map = mapOf("a" to 1, "b" to 2, "c" to 3)16map.shouldNotContainKeys("d", "e")17val map = mapOf("a" to 1, "b" to 2, "c" to 3)18map.shouldContainKey("a")Map.shouldNotContainValues
Using AI Code Generation
1val map = mapOf(1 to "one", 2 to "two", 3 to "three")2map.shouldNotContainValues("four", "five")3val map = mapOf(1 to "one", 2 to "two", 3 to "three")4map.shouldNotContainKeys(4, 5)5val map = mapOf(1 to "one", 2 to "two", 3 to "three")6map.shouldHaveKey(1)7val map = mapOf(1 to "one", 2 to "two", 3 to "three")8map.shouldNotHaveKey(4)9val map = mapOf(1 to "one", 2 to "two", 3 to "three")10map.shouldHaveValue("one")11val map = mapOf(1 to "one", 2 to "two", 3 to "three")12map.shouldNotHaveValue("four")13val map = mapOf(1 to "one", 2 to "two", 3 to "three")14map.shouldHaveSize(3)15val map = mapOf<Int, String>()16map.shouldBeEmpty()17val map = mapOf(1Learn 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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
