How to use Class.shouldHaveAnnotation method of io.kotest.matchers.types.matchers class

Best Kotest code snippet using io.kotest.matchers.types.matchers.Class.shouldHaveAnnotation

TypeMatchersTest.kt

Source:TypeMatchersTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.matchers.types2import io.kotest.assertions.throwables.shouldThrow3import io.kotest.core.spec.style.WordSpec4import io.kotest.matchers.collections.shouldHaveSize5import io.kotest.matchers.types.beInstanceOf6import io.kotest.matchers.types.beOfType7import io.kotest.matchers.types.beTheSameInstanceAs8import io.kotest.matchers.nulls.beNull9import io.kotest.matchers.nulls.shouldBeNull10import io.kotest.matchers.should11import io.kotest.matchers.shouldBe12import io.kotest.matchers.shouldNot13import io.kotest.matchers.types.haveAnnotation14import io.kotest.matchers.types.instanceOf15import io.kotest.matchers.types.shouldBeInstanceOf16import io.kotest.matchers.types.shouldBeSameInstanceAs17import io.kotest.matchers.types.shouldBeTypeOf18import io.kotest.matchers.types.shouldHaveAnnotation19import io.kotest.matchers.types.shouldNotBeInstanceOf20import io.kotest.matchers.types.shouldNotBeTypeOf21import java.util.ArrayList22import java.util.LinkedList23@Suppress("UnnecessaryVariable")24class TypeMatchersTest : WordSpec() {25 @Retention(AnnotationRetention.RUNTIME)26 annotation class Vod27 @Vod28 class Wibble29 init {30 "typeOf" should {31 "test for exact type" {32 val arrayList: List<Int> = arrayListOf(1, 2, 3)33 arrayList.shouldBeTypeOf<ArrayList<*>>()34 arrayList.shouldNotBeTypeOf<List<*>>()35 }36 }37 "haveAnnotation(annotation)" should {38 "test for the presence of an annotation" {39 Wibble::class.java should haveAnnotation(Vod::class.java)40 Wibble::class.java.shouldHaveAnnotation(Vod::class.java)41 }42 }43 "beInstanceOf" should {44 "test that value is assignable to class" {45 val arrayList: List<Int> = arrayListOf(1, 2, 3)46 arrayList should beInstanceOf(ArrayList::class)47 arrayList should beInstanceOf<ArrayList<*>>()48 arrayList should instanceOf(ArrayList::class)49 arrayList should instanceOf<ArrayList<*>>()50 arrayList.shouldBeInstanceOf<ArrayList<*>>()51 arrayList should beInstanceOf(List::class)52 arrayList should beInstanceOf<List<*>>()53 arrayList should instanceOf(List::class)54 arrayList should instanceOf<List<*>>()55 arrayList.shouldBeInstanceOf<List<*>>()56 shouldThrow<AssertionError> { arrayList should beInstanceOf(LinkedList::class) }57 shouldThrow<AssertionError> { arrayList should beInstanceOf(LinkedList::class) }58 shouldThrow<AssertionError> { arrayList should beInstanceOf<LinkedList<*>>() }59 shouldThrow<AssertionError> { arrayList should instanceOf(LinkedList::class) }60 shouldThrow<AssertionError> { arrayList should instanceOf<LinkedList<*>>() }61 shouldThrow<AssertionError> { arrayList.shouldBeInstanceOf<LinkedList<*>>() }62 arrayList.shouldNotBeInstanceOf<LinkedList<*>>()63 shouldThrow<AssertionError> { arrayList.shouldNotBeInstanceOf<ArrayList<*>>() }64 }65 "use smart contracts to cast" {66 val list: Collection<Int> = arrayListOf(1, 2, 3)67 list.shouldBeInstanceOf<ArrayList<Int>>()68 list.add(4) // this will only work if smart contracts worked69 }70 "return the receiver" {71 val list: Collection<Int> = arrayListOf(1, 2, 3)72 list.shouldBeInstanceOf<ArrayList<Int>>()73 .shouldHaveSize(3)74 }75 "Allow execution with a lambda" {76 val list = arrayListOf(1, 2, 3)77 list.shouldBeInstanceOf<ArrayList<Int>> {78 it shouldBeSameInstanceAs list79 }80 }81 "Returns typecasted value when invoked with a lambda" {82 val list = arrayListOf(1, 2, 3)83 val typecastedList = list.shouldBeInstanceOf<ArrayList<Int>> {}84 typecastedList shouldBeSameInstanceAs list85 }86 "accepts null values" {87 val arrayList: List<Int>? = null88 shouldThrow<AssertionError> { arrayList should beInstanceOf(ArrayList::class) }89 shouldThrow<AssertionError> { arrayList.shouldBeInstanceOf<ArrayList<*>>() }90 shouldThrow<AssertionError> { arrayList shouldNot beInstanceOf(List::class) }91 shouldThrow<AssertionError> { arrayList.shouldNotBeInstanceOf<LinkedList<*>>() }92 }93 }94 "beOfType" should {95 "test that value have exactly the same type" {96 val arrayList: List<Int> = arrayListOf(1, 2, 3)97 arrayList should beOfType<ArrayList<Int>>()98 shouldThrow<AssertionError> {99 arrayList should beOfType<LinkedList<Int>>()100 }101 shouldThrow<AssertionError> {102 arrayList should beOfType<List<Int>>()103 }104 }105 "Allow execution with a lambda" {106 val list: Any = arrayListOf(1, 2, 3)107 list.shouldBeTypeOf<ArrayList<Int>> {108 it shouldBeSameInstanceAs list109 it[0] shouldBe 1110 }111 }112 "Returns typecasted value when executed with a lambda" {113 val list: Any = arrayListOf(1, 2, 3)114 val typecastedList = list.shouldBeTypeOf<ArrayList<Int>> {}115 typecastedList shouldBeSameInstanceAs list116 typecastedList[0] shouldBe 1117 }118 "uses smart contracts to cast" {119 val list: Any = arrayListOf(1, 2, 3)120 list.shouldBeTypeOf<ArrayList<Int>>()121 list[0] shouldBe 1122 }123 "return the receiver" {124 arrayListOf(1, 2, 3)125 .shouldBeTypeOf<ArrayList<Int>>()126 .shouldHaveSize(3)127 }128 "accepts null values" {129 val arrayList: List<Int>? = null130 shouldThrow<AssertionError> { arrayList should beOfType<List<Int>>() }131 shouldThrow<AssertionError> { arrayList.shouldBeTypeOf<List<*>>() }132 shouldThrow<AssertionError> { arrayList shouldNot beOfType<List<Int>>() }133 shouldThrow<AssertionError> { arrayList.shouldNotBeTypeOf<List<*>>() }134 }135 }136 "TypeMatchers.theSameInstanceAs" should {137 "test that references are equal" {138 val b: List<Int>? = listOf(1, 2, 3)139 val a: List<Int>? = b140 val c: List<Int>? = listOf(1, 2, 3)141 a should beTheSameInstanceAs(b)142 a.shouldBeSameInstanceAs(b)143 shouldThrow<AssertionError> {144 a should beTheSameInstanceAs(c)145 }146 shouldThrow<AssertionError> {147 a.shouldBeSameInstanceAs(c)148 }149 }150 }151 "beTheSameInstanceAs" should {152 "test that references are equal" {153 val b = listOf(1, 2, 3)154 val a = b155 val c = listOf(1, 2, 3)156 a should beTheSameInstanceAs(b)157 shouldThrow<AssertionError> {158 a should beTheSameInstanceAs(c)159 }160 }161 }162 "beNull" should {163 val nullString: String? = null164 val nonNullString: String? = "Foo"165 "Pass for a null value" {166 nullString.shouldBeNull()167 nullString should beNull()168 }169 "Fail for a non-null value" {170 shouldThrow<AssertionError> { nonNullString.shouldBeNull() }171 shouldThrow<AssertionError> { nonNullString should beNull() }172 }173 }174 }175}...

Full Screen

Full Screen

matchers.kt

Source:matchers.kt Github

copy

Full Screen

1@file:JvmName("jvmTypeMatchersKt")2package io.kotest.matchers.types3import io.kotest.matchers.Matcher4import io.kotest.matchers.MatcherResult5import io.kotest.matchers.should6inline fun <A, reified T : Annotation> Class<A>.shouldHaveAnnotation(klass: Class<T>) = this should haveAnnotation(klass)7@Suppress("UNUSED_PARAMETER")8inline fun <A, reified T : Annotation> haveAnnotation(klass: Class<T>) = object : Matcher<Class<A>> {9 val className = T::class.qualifiedName10 override fun test(value: Class<A>): MatcherResult {11 val passed = value.annotations.any { it.annotationClass.qualifiedName == className }12 return MatcherResult(13 passed,14 { "Class $value should contain annotation $className" },15 {16 "Class $value should not contain annotation $className"17 })18 }19}...

Full Screen

Full Screen

Class.shouldHaveAnnotation

Using AI Code Generation

copy

Full Screen

1import io.kotest.matchers.types.*2class Student : Person()3fun shouldHaveAnnotationTest() {4val student = Student()5student.shouldHaveAnnotation<Serializable>()6}7import io.kotest.matchers.types.*8class Student : Person()9fun shouldNotHaveAnnotationTest() {10val student = Student()11student.shouldNotHaveAnnotation<Serializable>()12}13import io.kotest.matchers.types.*14class Student : Person()15fun shouldBeSubclassOfTest() {16val student = Student()17student.shouldBeSubclassOf<Person>()18}19import io.kotest.matchers.types.*20class Student : Person()21fun shouldNotBeSubclassOfTest() {22val student = Student()23student.shouldNotBeSubclassOf<Person>()24}25import io.kotest.matchers.types.*26data class Person(val name: String, val age: Int)27fun shouldBeDataClassTest() {28Person::class.shouldBeDataClass()29}30import io.kotest.matchers.types.*31class Person(val name: String, val age: Int)32fun shouldNotBeDataClassTest() {33Person::class.shouldNotBeDataClass()34}35import io.kotest.matchers.types.*36fun shouldBeSealedClassTest() {37Person::class.shouldBeSealedClass()38}39import io.kotest.matchers.types.*40fun shouldNotBeSealedClassTest() {41Person::class.shouldNotBeSealedClass()42}

Full Screen

Full Screen

Class.shouldHaveAnnotation

Using AI Code Generation

copy

Full Screen

1class Test {2@DisplayName("Should have annotation")3fun `should have annotation` () {4val actual = classWithAnnotation.shouldHaveAnnotation<Annotation>()5}6}7class ClassWithAnnotation {8fun method() {9}10}

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 method in matchers

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful