0

I'm trying to write a test based on just testCompile group: 'io.mockk', name: 'mockk', version: '1.7.15' but in the code below:

import io.mockk.every
import io.mockk.any
import io.mockk.Runs
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension

@ExtendWith(MockKExtension::class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class ConfigDistributorTest {
        @MockK
        lateinit var configService: ...

        @MockK
        lateinit var centralisedConfigRegisterService: ...

            val configDistributor =  ConfigDistributor(centralisedConfigRegisterService, configService)

            @Test
            fun shouldDistributeConfigToComponents(){
                every {
                    configService.readConfig(any())
                } just Runs
            }
        }

although Runs, MockK and MockKExtension are successfully imported, the every and any() are not available. Is io.mockk.any the correct import statement and which other dependency is required to use them?

Jason Harris
  • 53
  • 2
  • 9

1 Answers1

3

First you need to import every. import io.mockk.every is the correct way to do it. Inside of every any is automatically imported, so you don't need to do that. Other things looks fine

Please invalidates caches, re-import project. Probably you have some issues with IDE.

oleksiyp
  • 2,659
  • 19
  • 15
  • We're using https://bazel.build as our build system and its our own project I'm trying to use `io.mock` in. It's possible that we're not adding the right jars to the classpath. Could you confirm that we only need mockk-1.7.15.jar, mockk-dsl-jvm-1.7.15.jar and mockk-agent-1.7.15.jar on the classpath ? – Jason Harris Apr 21 '18 at 06:51
  • 1
    I can confirm that this is what I have in Gradle. +dependency kotlin-reflect – oleksiyp Apr 22 '18 at 08:11
  • Thanks: it look like its some strange Intellij/Kotlin situation: in a java class, `import static io.mockk.MockKKt.every;` is fine! – Jason Harris Apr 22 '18 at 10:08
  • Its sorted out now, thanks. It was an issue with how Kotlin dependencies are setup by `Bazel`. I'll be able to start using `io.mockk` properly now! – Jason Harris Apr 23 '18 at 11:30