我试图为我的kotlin spring应用程序编写一个集成测试。 为此,我正在使用kotlintest框架。由于我需要在应用程序中模拟其中一个bean,因此还添加了带有springmockk扩展名的ockk。添加springmockk扩展后,不再执行测试。
我注意到,只要将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")
}
在github上,我发现一个问题已经很不幸地解决了,没有任何适当的方式同时使用这两个框架:https://github.com/Ninja-Squad/springmockk/issues/26
编辑:
这是一个示例测试,在使用嘲笑时有效,但在使用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
输出:
> 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