简体   繁体   English

使用 springmockk 时 Kotlintest 不执行测试

[英]Kotlintest not executing test when using springmockk

I tried to write an integration test for my kotlin spring application.我试图为我的 kotlin spring 应用程序编写一个集成测试。 For this I am using the kotlintest framework.为此,我使用了 kotlintest 框架。 As I need to mock one of the beans in my application I also added mockk with the springmockk extension.因为我需要在我的应用程序中模拟其中一个 bean,所以我还添加了带有 springmockk 扩展的 mockk。 After adding the springmockk extension the test no longer got executed.添加 springmockk 扩展后,不再执行测试。

I noticed this happens as soon as springmockk is added to the gradle testImplement dependencies, it does not even have to be imported in the application code itself.我注意到只要将 springmockk 添加到 gradle testImplement 依赖项中就会发生这种情况,它甚至不必导入到应用程序代码本身中。

buildscript {
    ext.kotlin_version = '1.3.21'
    ext.kotlintestVersion='3.4.2'
    ext.spring_boot_version='2.1.4.RELEASE'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBoot_version")
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
    }
}

...

dependencies {
    ...
    testImplementation("org.springframework.boot:spring-boot-starter-test:$springBoot_version") {
    testImplementation("io.kotlintest:kotlintest-runner-junit5:$kotlintestVersion")
    testImplementation("io.kotlintest:kotlintest-extensions-spring:$kotlintestVersion")
    testImplementation("io.mockk:mockk:1.9.3")
//    testImplementation("com.ninja-squad:springmockk:2.0.0")
}

On github I found an issue which sadly has been closed already without any proper way of using these two frameworks together: https://github.com/Ninja-Squad/springmockk/issues/26在 github 上,我发现了一个问题,遗憾的是已经关闭了,但没有任何将这两个框架一起使用的正确方法: https : //github.com/Ninja-Squad/springmockk/issues/26

Edit:编辑:

This is an example test, which is working when using mockkito but not when using springmockk.这是一个示例测试,它在使用 mockkito 时有效,但在使用 springmockk 时无效。

@ExtendWith(SpringExtension::class)
@SpringBootTest
@AutoConfigureMockMvc
@WithMockUser(authorities = ["ROLE_TESTUSER"])
internal class MockTest : AnnotationSpec() {

    override fun listeners() = listOf(SpringListener)

    @Autowired
    lateinit var mockMvc: MockMvc

    @MockkBean
    lateinit var securityHelper: SecurityHelper

    @Test
    fun integrationTest() {
        whenever(securityHelper.someFunction()).thenReturn("test")
        mockMvc.perform(MockMvcRequestBuilders.get("/some/endpoint")
        ).andExpect(MockMvcResultMatchers.status().isOk)
    }
}

./gradlew test --rerun-tasks output: ./gradlew test --rerun-tasks输出:

> Configure project :
Property 'app.env' not found using profile dev: use -Papp.env=dev to define the environment for 'SPRING_PROFILES_ACTIVE'

> Task :compileKotlin

BUILD SUCCESSFUL in 56s
5 actionable tasks: 5 executed

Mock Bean with springMockk用 springMockk 模拟 Bean

To use @MockkBean you need to add springmockk and remove mockito core from the spring-boot-starter-test in your gradle file like:要使用@MockkBean您需要在 gradle 文件中添加springmockk并从 spring-boot-starter-test 中删除 mockito 核心,例如:

testImplementation("io.mockk:mockk:1.9.3")
testImplementation("com.ninja-squad:springmockk:2.0.2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
    exclude(module = "mockito-core")
}

Then your bean should be mocked with:那么你的 bean 应该被嘲笑:

@MockkBean
lateinit var securityHelper: SecurityHelper

Mock Bean with MockK only仅使用 MockK 的 Mock Bean

You can mock the bean just using mockK by modifying the @TestConfiguration and setting the profile of the mock Bean to be the same as the one used for your test:您可以通过修改@TestConfiguration并将模拟 Bean 的配置文件设置为与用于测试的配置文件相同,使用 mockK 来模拟 bean:

  @TestConfiguration
  class ControllerTestConfig {

    @Bean
    @Profile("test")
    fun securityHelper(): SecurityHelper {
      val securityHelperMock: SecurityHelper = mockk(relaxed = true)
      every { securityHelperMock.someFunction() } returns "test"
      return securityHelperMock
    }
  }

You can force use the TestConfig by putting it in your @SpringBootTest :您可以通过将其放入@SpringBootTest来强制使用 TestConfig :

@SpringBootTest(
    classes = [YourApplication::class, ControllerTestConfig::class]
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM