You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Kotlin中含特定枚举类型转换逻辑的函数编写单元测试

Kotlin单元测试实现方案

前置说明

采用Kotlin生态主流测试栈:JUnit 5 + MockK(Mock框架) + AssertJ(断言库),若使用Mockito等其他框架,测试逻辑一致仅API写法有差异。

先补全测试需要用到的基础类定义(和你原有业务逻辑对齐):

// 你原有代码中的枚举类
enum class Type1 { enum1, enum2, enum3 }
// 你原有代码中的实体类
data class Entity(val anotherProperty: List<Type1>)
// 你原有代码中的Repo接口
interface Repo {
    fun findById(id: String): Entity
}
// 你原有代码中的AnotherComponent接口
interface AnotherComponent {
    fun findByIdAndProperty(id: String, property: Type1): ReturnType
}
// 占位原代码中的返回类型,可替换为你实际的返回类型
class ReturnType

1. 独立测试toConvert扩展函数(全覆盖3个分支)

直接构造不同的List<Type1>输入,断言返回结果符合预期即可:

import org.junit.jupiter.api.Test
import org.assertj.core.api.Assertions.assertThat

class ToConvertTest {
    @Test
    fun `当列表包含enum1时返回enum1`() {
        val input = listOf(Type1.enum2, Type1.enum1, Type1.enum3)
        assertThat(input.toConvert()).isEqualTo(Type1.enum1)
    }

    @Test
    fun `当列表不包含enum1但包含enum2时返回enum2`() {
        val input = listOf(Type1.enum2, Type1.enum3)
        assertThat(input.toConvert()).isEqualTo(Type1.enum2)
    }

    @Test
    fun `当列表不包含enum1和enum2时返回enum3`() {
        val input = listOf(Type1.enum3)
        // 空列表也走该分支,可额外补充空列表测试用例
        assertThat(input.toConvert()).isEqualTo(Type1.enum3)
        assertThat(emptyList<Type1>().toConvert()).isEqualTo(Type1.enum3)
    }
}

2. 测试myFunction逻辑(验证anotherComponent入参正确性)

通过Mock依赖的repo和anotherComponent,验证最终调用anotherComponent.findByIdAndProperty时传入的参数符合预期:

import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import org.junit.jupiter.api.Test

class MyFunctionTest {
    // Mock依赖对象
    private val mockRepo = mockk<Repo>()
    private val mockAnotherComponent = mockk<AnotherComponent>()
    // 初始化要测试的类,替换MyService为你实际的业务类名
    private val testService = MyService(mockRepo, mockAnotherComponent)

    private val testId = "test_id_123"

    @Test
    fun `当entity属性包含enum1时,anotherComponent收到参数enum1`() {
        // 构造测试数据
        val testEntity = Entity(anotherProperty = listOf(Type1.enum1, Type1.enum3))
        // Mock repo返回结果
        every { mockRepo.findById(testId) } returns testEntity
        // Mock anotherComponent返回值,不需要关心具体返回内容时可直接返回mock对象
        every { mockAnotherComponent.findByIdAndProperty(any(), any()) } returns mockk()

        // 执行测试方法
        testService.myFunction(testId)

        // 验证调用参数符合预期
        verify(exactly = 1) {
            mockAnotherComponent.findByIdAndProperty(testId, Type1.enum1)
        }
    }

    @Test
    fun `当entity属性仅包含enum2时,anotherComponent收到参数enum2`() {
        val testEntity = Entity(anotherProperty = listOf(Type1.enum2, Type1.enum3))
        every { mockRepo.findById(testId) } returns testEntity
        every { mockAnotherComponent.findByIdAndProperty(any(), any()) } returns mockk()

        testService.myFunction(testId)

        verify(exactly = 1) {
            mockAnotherComponent.findByIdAndProperty(testId, Type1.enum2)
        }
    }

    @Test
    fun `当entity属性不包含enum1和enum2时,anotherComponent收到参数enum3`() {
        val testEntity = Entity(anotherProperty = listOf(Type1.enum3))
        every { mockRepo.findById(testId) } returns testEntity
        every { mockAnotherComponent.findByIdAndProperty(any(), any()) } returns mockk()

        testService.myFunction(testId)

        verify(exactly = 1) {
            mockAnotherComponent.findByIdAndProperty(testId, Type1.enum3)
        }
    }
}

内容的提问来源于stack exchange,提问作者user572490

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 12:48:00