首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用springmockk时Kotlintest不执行测试

使用springmockk时Kotlintest不执行测试
EN

Stack Overflow用户
提问于 2020-01-24 17:48:15
回答 1查看 635关注 0票数 5

我试图为我的kotlin spring应用程序编写一个集成测试。为此,我使用kotlintest框架。因为我需要模拟应用程序中的一个bean,所以我还用springmockk扩展添加了mockk。在添加了springmockk扩展之后,测试不再被执行。

我注意到,一旦springmockk被添加到gradle testImplement依赖项中,就会发生这种情况,它甚至不需要导入到应用程序代码本身中。

代码语言:javascript
复制
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")
}

在github上,我发现了一个问题,不幸的是,这个问题已经被解决了,没有任何适当的方法来同时使用这两个框架:https://github.com/Ninja-Squad/springmockk/issues/26

编辑:

这是一个示例测试,它在使用mockkito时有效,但在使用springmockk时无效。

代码语言:javascript
复制
@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输出:

代码语言:javascript
复制
> 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
EN

回答 1

Stack Overflow用户

发布于 2020-07-07 22:46:43

使用springMockk模拟Bean

要使用@MockkBean,您需要添加springmockk,并从gradle文件的spring-boot-starter-test中删除mockito核心,如下所示:

代码语言:javascript
复制
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")
}

那么你的bean应该用以下代码来模拟:

代码语言:javascript
复制
@MockkBean
lateinit var securityHelper: SecurityHelper

仅使用MockK的模拟Bean

通过修改@TestConfiguration并将mock bean的配置文件设置为与用于测试的配置文件相同,您可以只使用mockK来模拟Bean:

代码语言:javascript
复制
  @TestConfiguration
  class ControllerTestConfig {

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

您可以通过将TestConfig放入@SpringBootTest来强制使用它

代码语言:javascript
复制
@SpringBootTest(
    classes = [YourApplication::class, ControllerTestConfig::class]
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59893918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档