Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error "A mock for the type X was not generated." #59

Open
lammertw opened this issue Mar 8, 2023 · 11 comments
Open

Getting error "A mock for the type X was not generated." #59

lammertw opened this issue Mar 8, 2023 · 11 comments

Comments

@lammertw
Copy link

lammertw commented Mar 8, 2023

I have an interface called Functions:

interface Functions {
}

In my test class, I try to create a mock for it:

@Mock
private val functions = mock(classOf<Functions>())

When running the test it fails with:


A mock for the type Functions was not generated.

    Make sure the property holding the mock is annotated with @Mock:

        @Mock
        private val myMock = mock(classOf<Functions>())
    

io.mockative.NoSuchMockError: A mock for the type Functions was not generated.

    Make sure the property holding the mock is annotated with @Mock:

        @Mock
        private val myMock = mock(classOf<Functions>())

I've followed the instructions from the readme for the build file, however just adding the tsp plugin without version didn't work for me. So instead I have:

plugins {
  id("com.google.devtools.ksp").version("1.8.10-1.0.9")
}

The rest is the same as in the readme:

kotlin {
    sourceSets {
        val commonTest by getting {
            dependencies {
                implementation("io.mockative:mockative:1.3.1")
            }
        }
    }
}

dependencies {
    configurations
        .filter { it.name.startsWith("ksp") && it.name.contains("Test") }
        .forEach {
            add(it.name, "io.mockative:mockative-processor:1.3.1")
        }
}
@lammertw
Copy link
Author

lammertw commented Mar 8, 2023

Some further details, when printing all configurations I noticed there isn't any configuration that starts with "ksp" and contains "Test".
Here is a full list:

allSourceSetsCompileDependenciesMetadata
allSourceSetsRuntimeDependenciesMetadata
androidApis
androidJdkImage
androidTestAnnotationProcessor
androidTestCompileOnly
androidTestDebugAnnotationProcessor
androidTestDebugCompileOnly
androidTestDebugImplementation
androidTestDebugRuntimeOnly
androidTestDebugWearApp
androidTestImplementation
androidTestReleaseAnnotationProcessor
androidTestReleaseCompileOnly
androidTestReleaseImplementation
androidTestReleaseRuntimeOnly
androidTestReleaseWearApp
androidTestRuntimeOnly
androidTestUtil
androidTestWearApp
annotationProcessor
api
archives
commonMainApi
commonMainApiDependenciesMetadata
commonMainCompileOnly
commonMainCompileOnlyDependenciesMetadata
commonMainImplementation
commonMainImplementationDependenciesMetadata
commonMainIntransitiveDependenciesMetadata
commonMainRuntimeOnly
commonMainRuntimeOnlyDependenciesMetadata
commonTestApi
commonTestApiDependenciesMetadata
commonTestCompileOnly
commonTestCompileOnlyDependenciesMetadata
commonTestImplementation
commonTestImplementationDependenciesMetadata
commonTestIntransitiveDependenciesMetadata
commonTestRuntimeOnly
commonTestRuntimeOnlyDependenciesMetadata
compileOnly
coreLibraryDesugaring
debugAnnotationProcessor
debugApi
debugCompileOnly
debugImplementation
debugRuntimeOnly
debugWearApp
default
implementation
kotlinCompilerClasspath
kotlinCompilerPluginClasspath
kotlinCompilerPluginClasspathMetadataMain
kotlinKlibCommonizerClasspath
kotlinNativeCompilerPluginClasspath
ksp
kspCommonMainMetadata
lintChecks
lintPublish
metadataApiElements
metadataCompilationApi
metadataCompilationCompileOnly
metadataCompilationImplementation
metadataCompileClasspath
releaseAnnotationProcessor
releaseApi
releaseCompileOnly
releaseImplementation
releaseRuntimeOnly
releaseWearApp
runtimeOnly
testAnnotationProcessor
testCompileOnly
testDebugAnnotationProcessor
testDebugCompileOnly
testDebugImplementation
testDebugRuntimeOnly
testDebugWearApp
testFixturesAnnotationProcessor
testFixturesApi
testFixturesCompileOnly
testFixturesDebugAnnotationProcessor
testFixturesDebugApi
testFixturesDebugCompileOnly
testFixturesDebugImplementation
testFixturesDebugRuntimeOnly
testFixturesDebugWearApp
testFixturesImplementation
testFixturesReleaseAnnotationProcessor
testFixturesReleaseApi
testFixturesReleaseCompileOnly
testFixturesReleaseImplementation
testFixturesReleaseRuntimeOnly
testFixturesReleaseWearApp
testFixturesRuntimeOnly
testFixturesWearApp
testImplementation
testReleaseAnnotationProcessor
testReleaseCompileOnly
testReleaseImplementation
testReleaseRuntimeOnly
testReleaseWearApp
testRuntimeOnly
testWearApp
wearApp

@magnumrocha
Copy link

After update to version 1.3.1, I'm having the same Issue.

@lammertw did you already find a fix for it?

@lammertw
Copy link
Author

@magnumrocha No, I never got this solved and switched to MocKMP instead.

@Nillerr
Copy link
Collaborator

Nillerr commented Mar 28, 2023

Hi @lammertw

I apologize for my long hiatus from this project. I have recently picked up the open issues and while investigating yours I haven't been able to replicate the behavior. It could have been a Kotlin / KSP 1.8 compatibility issue, but despite it meaning you changed to MocKMP, I am happy that you have found a solution.

@magnumrocha You mention you see the same issue. I will release an update to Mockative within the next 2 days, which updates Kotlin to 1.8.10 as well as updates KSP. Will you care to test whether that works for you? I will ping you in here once the release is available. Should that not work please share some related snippets of code from your project so I can have a look at it.

Thanks

@magnumrocha
Copy link

@Nillerr cool, I'll wait to test the update.

@Nillerr
Copy link
Collaborator

Nillerr commented Mar 29, 2023

Hi again @magnumrocha. I have released v1.4.0 this morning. Please let me know if this addresses the issue you're seeing,

@akiya-nagatsuka
Copy link

Having the same error on v1.4.1

@Nillerr
Copy link
Collaborator

Nillerr commented Aug 17, 2023

Are you guys still experiencing this issue?

@hieuwu
Copy link

hieuwu commented Sep 19, 2023

Are you guys still experiencing this issue?

I still face this issue. I run test on my Macbook M1 Pro chip

@HannahShulman
Copy link

Hello, I am facing the same error, on a KMM project, version 2.0.1, using a Macbook M1 Pro.
Would appreciate an update.
Thanks

@flandreas
Copy link

I was facing the same problem with my Kotlin Multiplatform project (JVM/JS) using version 2.0.1 and Kotlin 1.9.0 (also on MacBook M1 Pro, although I don't think that matters).

I guess in my the case the issue was related to my project structure. I'm using Gradle subprojects, each of which as a multiplatform structure.

This build.gradle.kts worked for me (extract):

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    kotlin("multiplatform") version "1.9.0" apply false
    id("com.google.devtools.ksp") version "1.9.0-1.0.13"
}

subprojects {
    apply(plugin = "org.jetbrains.kotlin.multiplatform")
    apply(plugin = "com.google.devtools.ksp")

    configure<KotlinMultiplatformExtension> {
        jvm()
        
        sourceSets {
            val commonTest by getting {
                dependencies {
                    implementation(kotlin("test-common"))
                    implementation("io.mockative:mockative:2.0.1")
                }
            }
        }

        dependencies {
            configurations
                .filter { it.name.startsWith("ksp") && it.name.contains("Test") }
                .forEach {
                    add(it.name, "io.mockative:mockative-processor:2.0.1")
                }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants