I'm trying to use "runTest()" in Kotlin multiplatform. I'm using Jetbrains's "Getting started"-project as an example. (https://kotlinlang.org/docs/multiplatform-library.html)
The problem is that runTest() does not find a coroutine context. It gives me the following build error:
Cannot access class 'kotlin.coroutines.CoroutineContext'. Check your module classpath for missing or conflicting dependencies
Here is my test:
class Base64JvmTest {
#OptIn(ExperimentalCoroutinesApi::class)
#Test
fun testNonAsciiString() {
runTest {
val utf8String = "Gödel"
val actual = Base64Factory.createEncoder().encodeToString(utf8String.toByteArray())
assertEquals("R8O2ZGVs", actual)
}
}
}
In build.gradle.kts, I set the following in kotlin.sourceSets:
val jvmTest by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
}
}
Please help me out - what am I missing?
As it turns out, there was an issue with Idea. I added the following dependency to get rid of the error:
dependencies {
commonTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
}
It shouldn't really be needed, as the common tests are not dependent on coroutines, but an acceptable work-around.
you can run runTest following way as documentation is suggested
#Test
fun exampleTest() = runTest {
val deferred = async {
delay(1_000)
async {
delay(1_000)
}.await()
}
deferred.await() // result available immediately
}
documentation code link
Related
I have created a simple Micronaut Kotlin Coroutines example want to write tests with kotlin-corotines-test. I have added the kotlin-corotines-test in dependencies.
I tried to use runBlockingTest, and the following test(Kotest/FuncSpec) failed.
#Test
fun `test GET all posts endpoint`() = runBlockingTest {
val response = client.exchange("/posts", Array<Post>::class.java).awaitSingle()
response.status shouldBe HttpStatus.OK
response.body()!!.map { it.title }.forAny {
it shouldContain "Micronaut"
}
}
And throw exceptions like this.
java.lang.IllegalStateException: This job has not completed yet
at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1190)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45)
at com.example.ApplicationTest.test GET posts endpoint(ApplicationTest.kt:30)
But if use runBlocking in the fun body, it works.
#Test
fun `test GET all posts endpoint`() {
runBlocking {
val response = client.exchange("/posts", Array<Post>::class.java).awaitSingle()
response.status shouldBe HttpStatus.OK
response.body()!!.map { it.title }.forAny {
it shouldContain "Micronaut"
}
}
}
Update: get the solution from issue Kotlin/kotlinx.coroutines#1204, update to kotlin coroutine to 1.6.0-RC to resolve it, and use runTest instead of the deprecated runBlockingTest.
This is a known issue and you can check here.
Just to document it here as well, there are few workarounds as:
Use InstantTaskExecutorRule
#get:Rule
val instantExecutorRule = InstantTaskExecutorRule()
Use runBlocking
#Test
fun test() = runBlocking {
coroutineJob.join()
}
Make sure you call from your test dispatcher instance
coroutineRule.testDispatcher.runBlockingTest{...}
I try to write a kotlin multiplatform library (android and ios) that uses ktor. Thereby I experience some issues with kotlins coroutines:
When writing tests I always get kotlinx.coroutines.JobCancellationException: Parent job is Completed; job=JobImpl{Completed}#... exception.
I use ktors mock engine for my tests:
client = HttpClient(MockEngine)
{
engine
{
addHandler
{ request ->
// Create response object
}
}
}
A sample method (commonMain module) using ktor. All methods in my library are written in a similar way. The exception occures if client.get is called.
suspend fun getData(): Either<Exception, String> = coroutineScope
{
// Exception occurs in this line:
val response: HttpResponse = client.get { url("https://www.google.com") }
return if (response.status == HttpStatusCode.OK)
{
(response.readText() as T).right()
}
else
{
Exception("Error").left()
}
}
A sample unit test (commonTest module) for the above method. The assertTrue statement is never called since the exception is thrown before.
#Test
fun getDataTest() = runTest
{
val result = getData()
assertTrue(result.isRight())
}
Actual implementation of runTest in androidTest and iosTest modules.
actual fun<T> runTest(block: suspend () -> T) { runBlocking { block() } }
I thought when I use coroutineScope, it waits until all child coroutines are done. What am I doing wrong and how can I fix this exception?
you can't cache HttpClient of CIO in client variable and reuse, It would be best if change the following code in your implementation.
val client:HttpClient get() = HttpClient(MockEngine) {
engine {
addHandler { request ->
// Create response object
}
}
}
The library must be updated, this glitch is in the fix report here: https://newreleases.io/project/github/ktorio/ktor/release/1.6.1
The problem is that you cannot use the same instance of the HttpClient. My ej:
HttpClient(CIO) {
install(JsonFeature) {
serializer = GsonSerializer()
}
}.use { client ->
return#use client.request("URL") {
method = HttpMethod.Get
}
}
I am trying to use Kotlin runCatching feature along with response + error translation functions. Something like
class SomeClassThatCallsServiceX {
fun remoteCall(input): TransformedResult {
kotlin.runCatching{ serviceX.call(input)}
.onSuccess { return functionToTranslateServiceXResponse(it)}
.onFailure{ throw functionToranslateServiceXError(it)}
}
}
functionToTranslateServiceXResponse : (OriginalResult) -> TransformedResult
functionToranslateServiceXError : (Throwable) -> RuntimeException
However Kotlin is giving me the following error - A 'return' expression required in a function with a block body ('{...}'). If you got this error after the compiler update, then it's most likely due to a fix of a bug introduced in 1.3.0 (see KT-28061 for details)
Running with Kotlin 1.4.
I am still new to Kotlin- wondering if I am doing something fundamentally wrong here. Any help would be greatly appreciated.
Update: tried the following pattern, basic test cases seem to be working fine. Would love to know what others think of the pattern
class SomeClassThatCallsServiceX {
fun remoteCall(input): TransformedResult {
return kotlin.runCatching{ serviceX.call(input)}
.mapCatching(functionToTranslateServiceXResponse)
.onFailure(functionToranslateServiceXError)
.getOrThrow()
}
}
functionToTranslateServiceXResponse : (OriginalResult) -> TransformedResult
functionToranslateServiceXError : (Throwable) -> Unit
I think there are couple of issues here:
class SomeClassThatCallsServiceX {
// You are not returning `TransformedResult` but Result<TransformedResult>
// but Result cannot be returned by kotlin https://stackoverflow.com/q/52631827/906265
fun remoteCall(input) {
// error mentioned that "return" was missing but you cannot return Result
kotlin.runCatching{ serviceX.call(input)}
.onSuccess { return functionToTranslateServiceXResponse(it) }
.onFailure{ throw functionToranslateServiceXError(it) }
}
}
Having a callback could be a solution (pseudocode):
fun remoteCall(input Any, callback: Callback) {
kotlin.runCatching{ serviceX.call(input)}
.onSuccess { callback.success(it) }
.onFailure { callback.error(it) }
}
Have a tiny example on Kotlin Playground website based on the above https://pl.kotl.in/W8wMDEzN1
I am trying to write my first Lint rule. For now I just want to detect the
use of the annotation #AnyThread.
I have created a module to implement my custom rule. The gradle file for this module is (I use the gradle plugin version 3.6.1):
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
compileOnly 'com.android.tools.lint:lint-api:26.6.1'
compileOnly 'com.android.tools.lint:lint-checks:26.6.1'
testImplementation "com.android.tools.lint:lint:26.6.1"
testImplementation "com.android.tools.lint:lint-tests:26.6.1"
testImplementation "com.android.tools:testutils:26.6.1"
testImplementation "junit:junit:4.12"
}
jar {
manifest {
attributes("Lint-Registry-v2": "com.test.lint.MyIssueRegistry")
}
}
My detector is:
package com.test.lint
//...
class AnyThreadAnnotationDetector: AbstractAnnotationDetector(), Detector.UastScanner {
companion object {
private const val AnyThreadId = "AnyThreadId"
const val AnyThreadDescription = "This is an attempt to find AnyThread annotation in code"
const val AnyThreadExplanation = "AnyThread annotation found!"
val ANYTHREAD_ANNOTATION_ISSUE = Issue.create(
id = AnyThreadId,
briefDescription = AnyThreadDescription,
explanation = AnyThreadExplanation,
category = Category.CORRECTNESS,
priority = 4,
severity = Severity.INFORMATIONAL,
implementation = Implementation(
AnyThreadAnnotationDetector::class.java,
Scope.JAVA_FILE_SCOPE
)
)
}
override fun applicableAnnotations(): List<String>? = listOf("androidx.annotation.AnyThread")
override fun visitAnnotationUsage(
context: JavaContext,
usage: UElement,
type: AnnotationUsageType,
annotation: UAnnotation,
qualifiedName: String,
method: PsiMethod?,
annotations: List<UAnnotation>,
allMemberAnnotations: List<UAnnotation>,
allClassAnnotations: List<UAnnotation>,
allPackageAnnotations: List<UAnnotation>
) {
context.report(
issue = ANYTHREAD_ANNOTATION_ISSUE,
scope = usage,
location = context.getNameLocation(usage),
message = "A message"
)
}
}
My IssueRegistry is:
class MyIssueRegistry : IssueRegistry() {
override val issues: List<Issue>
get() = listOf(
AnyThreadAnnotationDetector.ANYTHREAD_ANNOTATION_ISSUE)
override val api: Int = CURRENT_API
}
I wrote some tests:
class AnyThreadAnnotationDetectorTest {
#Test
fun noAnnotatedFileKotlin() {
TestLintTask.lint()
.files(
LintDetectorTest.kotlin(
"""
|package foo;
|
|class XmlHttpRequest {
|}""".trimMargin()
)
)
.allowMissingSdk()
.issues(AnyThreadAnnotationDetector.ANYTHREAD_ANNOTATION_ISSUE)
.run()
.expectClean()
}
#Test
fun annotatedKotlinMethod() {
TestLintTask.lint()
.files(
LintDetectorTest.kotlin(
"""
|package foo;
|
|import androidx.annotation.AnyThread
|
|class XmlHttpRequest {
|#AnyThread
|fun test(){}
|}""".trimMargin()
)
)
.allowMissingSdk()
.issues(AnyThreadAnnotationDetector.ANYTHREAD_ANNOTATION_ISSUE)
.run()
.expect(
"""
Just a test to find annotations
0 errors, 0 warnings
""".trimIndent()
)
}
#Test
fun testNoisyDetector() {
TestLintTask.lint().files(Stubs.ANYTHREAD_EXPERIMENT)
.allowMissingSdk()
.issues(AnyThreadAnnotationDetector.ANYTHREAD_ANNOTATION_ISSUE)
.run()
.expect(
"""
Just a test to find annotations
0 errors, 0 warnings
""".trimIndent()
)
}
}
Where the Stubs.ANYTHREAD_EXPERIMENT is:
object Stubs {
val ANYTHREAD_EXPERIMENT = kotlin(
"com/test/applicationlintdemoapp/AnythreadAnnotationStubs.kt",
"""
package com.test.applicationlintdemoapp
import androidx.annotation.AnyThread
class AnythreadClassExperiment {
#AnyThread
fun setTimeToNow() {
TimeTravelProvider().setTime(System.currentTimeMillis())
}
#AnyThread
fun setTimeToEpoch() {
TimeTravelProvider().setTime(0)
}
fun violateTimeTravelAccords() {
TimeTravelProvider().setTime(-1)
}
}
"""
).indented().within("src")
}
All my test fail (except noAnnotatedFileKotlin), actually if I put a breakpoint on
the call to context.report the test made in debug mode is never paused, meaning
that the annotation androidx.annotation.AnyThread is never detected.
What could go wrong ? what did I miss?
I have seen and read a some docs:
Writing custom lint rules
KotlinConf 2017 - Kotlin Static Analysis with Android Lint
Writing your first Lint check
Making Custom Lint for Kotlin Code
Getting the Most Out of Android Lint
Coding in Style: Static Analysis with Custom Lint Rules
And I controlled the configuration by implementing the NoisyDetector given
in the talk Coding in Style: Static Analysis with Custom Lint Rules, the result
of the test are fine.
I might be a little late to answer this, but it might be useful for other people who run into this question
I'm having the same problem, I need to find usages of an Annotation and report them. But for some reason the Kotlin UAST (Java works fine) doesn't record/report annotations. I'm using a sort of workaround to get through this
Workaround
Instead of visiting annotations, I'm visiting UMethod or UClass depending on what you need. Then I'm doing a manual String.contains() check on the node.sourcePsi.text to see if the annotation is there
override fun getApplicableUastTypes() = listOf(UMethod::class.java)
override fun createUastHandler(context: JavaContext): UElementHandler {
return object : UElementHandler() {
override fun visitMethod(node: UMethod) {
if (!shouldSkip(node.sourcePsi) && node.isAnnotatedWith("AnyThread")) {
context.report(
issue = ANYTHREAD_ANNOTATION_ISSUE,
scope = usage,
location = context.getNameLocation(usage),
message = "A message"
)
}
}
}
// Skip KtClass, because it calls the `visitMethod` method since the class has the constructor method in it
private fun shouldSkip(node: PsiElement?): Boolean = node is KtClass
}
fun UAnnotated.isAnnotatedWith(annotation: String) = sourcePsi?.text?.contains("#$annotation") == true
Drawbacks
The problem I see with this is that it will be called for every method instead of only when the annotation is found, and the shouldSkip() method seems like a hack to me. But other than that it works correctly and should report problems
Note: Calling node.hasAnnotation(), node.findAnnotation() or context.evaluator.hasAnnotation() will not find annotations in Kotlin
You can add stubs for the #AnyThread annotation by adding SUPPORT_ANNOTATIONS_JAR to the lint().files(...) call or manually declaring the #AnyThread annotation class in a separate test source file.
An example of using SUPPORT_ANNOTATIONS_JAR inside of CheckResultDetectorTest can be found here.
Today I stumbled across a situation which I do not understand, possibly because of lack of insight into how mockito and mockito-kotlin work internally.
Given the code below, from my Kotlin beginner perspective, I have two pretty similar interface-methods. One returns Boolean, one String. Both are suspend functions in my example as in my real world situation my functions are too.
class ITestInterface {
suspend fun returnBoolean(): Boolean {
return true
}
suspend fun returnSomeString() : String {
return "String"
}
}
#Test
fun demoTest() {
val implMock = mock<ITestInterface> {
on {
runBlocking {
returnSomeString()
}
} doReturn "Hello"
on {
runBlocking {
returnBoolean()
}
} doReturn false
}
}
My observation is, when I run the test, like depicted above, I get the following error Message
com.nhaarman.mockitokotlin2.MockitoKotlinException: NullPointerException thrown when stubbing.
This may be due to two reasons:
- The method you're trying to stub threw an NPE: look at the stack trace below;
- You're trying to stub a generic method: try `onGeneric` instead.
at com.nhaarman.mockitokotlin2.KStubbing.on(KStubbing.kt:72)
at com.rewedigital.fulfillment.picking.components.substitute.DemoTest.demoTest(DemoTest.kt:30)
[...]
Experiments showed that
the behavior is only shown by the Boolean returning function, not by returnSomeString()
removing the suspend keyword fro the returnBoolean function makes the error go away
using onGeneric as suggested in the error message also makes the error go away
Could anybody explain why this is happening? To what extend do we have to do with generics here? And what would be the correct way of solving the issue in our real application? Having a bunch of on {} and some onGeneric {} ?
Thanks for your help!
You should use the onBlocking method to properly mock the suspend function
Please try the following code:
#Test
fun demoTest() {
val implMock = mock<ITestInterface> {
onBlocking {
returnSomeString()
} doReturn "Hello"
onBlocking {
returnBoolean()
} doReturn false
}
runBlocking {
// Execute your code here
assertThat(implMock.returnSomeString()).isEqualTo("Hello")
assertThat(implMock.returnBoolean()).isEqualTo(false)
}
}