Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 340 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
kotlin-test: How to test for a specific type like: "is y instance of X"

#1
How to test if a val/var is of an expected type?

Is there something I am missing in Kotlin Test, like:

value shouldBe instanceOf<ExpectedType>()

Here is how I implemented it:

inline fun <reified T> instanceOf(): Matcher<Any> {
return object : Matcher<Any> {
override fun test(value: Any) =
Result(value is T, "Expected an instance of type: ${T::class} \n Got: ${value::class}", "")

}
}
Reply

#2
In KotlinTest, a lot is about proper spacing :)
You can use `should` to get access to a variety of built-in matchers.

import io.kotlintest.matchers.beInstanceOf
import io.kotlintest.should

value should beInstanceOf<Type>()

There is also an alternative syntax:

value.shouldBeInstanceOf<Type>()

See [here](

[To see links please register here]

) for more information.
Reply

#3
Since Kotlin 1.5 there is a nice solution included in kotlin-test:

```kotlin
assertIs<TypeExpected>(value)
```

This will not only assert that `value` is of type `TypeExpected`, it will also smart cast `value`, so that you can access all methods of `TypeExpected`. Just include the dependency, e.g:

```groovy
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:1.7.10")
```

And you can do stuff like this

```kotlin
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs

class AssertIsTests {

@Test
fun `test for list`() {
val list = Collector.List.collect(1, 1, 2, 2, 3)
assertEquals(5, list.size)

// assertEquals(1, list[0]) won't compile: list is of type Collection<Int> which doesn't support []
assertIs<List<Int>>(list) // from now on list is of type List<Int>!
assertEquals(4, list.indexOf(3)) // now there are all list methods
assertEquals(1, list[0]) // including indexed getters
}

@Test
fun `test for set`() {
val set = Collector.Set.collect(1, 1, 2, 2, 3)
assertEquals(3, set.size) // Set with duplicates removed

assertIs<Set<Int>>(set) // from now on set is of Type Set<Int>
}
}

enum class Collector {
List {
override fun collect(vararg args: Int) = args.toList()
},
Set {
override fun collect(vararg args: Int) = args.toSet()
};

abstract fun collect(vararg args: Int): Collection<Int>
}
```
Reply

#4
In 2020 Kotlintest library was renamed to Kotest and package names changed accordingly. So I decided to publish an updated version of the @TheOperator's answer.

To check that a `value` is *an instance or a subtype of* `Type`:

import io.kotest.matchers.should
import io.kotest.matchers.types.beInstanceOf

value should beInstanceOf<Type>()

An alternative to this is a method from [Core Matchers][1]:

import io.kotest.matchers.types.shouldBeInstanceOf

val castedValue: Type = value.shouldBeInstanceOf<Type>()

Note that in this case a value casted to the specified type is returned, which can be used in further validations.

----------

In case you need *exact type matching*, use another set of methods:

import io.kotest.matchers.should
import io.kotest.matchers.types.beOfType
import io.kotest.matchers.types.shouldBeTypeOf

value should beOfType<Type>()
val castedValue: Type = value.shouldBeTypeOf<Type>()


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through