0

概述

预期行为

使用JUnit 5 的注释语法初始化mockObject函数实现替换模拟对象初始化,如文档@oleksiyp的Medium 帖子中所述。

当前行为

有问题的测试是@phauer为 JUnit 5概述的参数化测试,似乎与. 为了使用 LiveData 实现测试,测试必须使用@JeroenMols设计的在本地单元测试中同步运行。@ExtendWith(MockKExtension::class)InstantExecutorExtension

模拟对象初始化与mockObject函数按预期工作,但使用注释失败@MockK

错误

警告消息/构建失败:

尚不支持具有非 SOURCE 保留的可重复注释。

执行

mockObject功能实现(按预期工作)

@ExtendWith(InstantExecutorExtension::class)
class NavigateContentTests {
    private val mainThreadSurrogate = newSingleThreadContext("UI thread")
    private val contentViewModel = ContentViewModel()

    // This is the stream of tests to run in the Parameterized test below.
    private fun NavigateContent() = Stream.of(
            NavigateContentTest(
                    isRealtime = false,
                    feedType = MAIN,
                    timeframe = DAY,
                    mockFeedList = mockDbContentListForDay,
                    mockContent = mockArticleContent),
            ...)

    @BeforeAll
    fun beforeAll() { mockkObject(ContentRepository) }

    @AfterAll
    fun afterAll() { unmockkAll() // Re-assigns transformation of object to original state prior to mock. }

    @BeforeEach
    fun beforeEach() { Dispatchers.setMain(mainThreadSurrogate) }

    @AfterEach
    fun afterEach() {
        Dispatchers.resetMain() // Reset main dispatcher to the original Main dispatcher.
        mainThreadSurrogate.close()
    }

    @ParameterizedTest
    @MethodSource("NavigateContent")
    fun `Navigate Content`(test: NavigateContentTest) = runBlocking {
        every { ContentRepository.getMainFeedList(test.isRealtime, any()) } returns mockGetMainFeedList(
                test.mockFeedList, CONTENT)
        every {
            ContentRepository.queryLabeledContentList(test.feedType)
        } returns mockQueryMainContentList(test.mockFeedList)
        every { ContentRepository.getContent(test.mockContent.id) } returns mockGetContent(test)
        // Tests here...
        // Verification here...
    }
}

注释语法初始化(由于两个扩展而不起作用@ExtendWith

@ExtendWith(InstantExecutorExtension::class)
@ExtendWith(MockKExtension::class)
class NavigateContentTests {

    // This object should be mocked.
    @MockK
    lateinit var contentRepository: ContentRepository

    private val mainThreadSurrogate = newSingleThreadContext("UI thread")
    private val contentViewModel = ContentViewModel()

    // This is the stream of tests to run in the Parameterized test below.
    private fun NavigateContent() = Stream.of(
            NavigateContentTest(
                    isRealtime = false,
                    feedType = MAIN,
                    timeframe = DAY,
                    mockFeedList = mockDbContentListForDay,
                    mockContent = mockArticleContent),
            ...)

    @BeforeAll
    fun beforeAll() {  MockKAnnotations.init(this, relaxUnitFun = true) // turn relaxUnitFun on for }

    @AfterAll
    fun afterAll() { unmockkAll() // Re-assigns transformation of object to original state prior to mock. }

    @BeforeEach
    fun beforeEach() { Dispatchers.setMain(mainThreadSurrogate) }

    @AfterEach
    fun afterEach() {
        Dispatchers.resetMain() // Reset main dispatcher to the original Main dispatcher.
        mainThreadSurrogate.close()
    }

    @ParameterizedTest
    @MethodSource("NavigateContent")
    fun `Navigate Content`(test: NavigateContentTest) = runBlocking {
        every { contentRepository.getMainFeedList(test.isRealtime, any()) } returns mockGetMainFeedList(
                test.mockFeedList, CONTENT)
        every {
            contentRepository.queryLabeledContentList(test.feedType)
        } returns mockQueryMainContentList(test.mockFeedList)
        every { contentRepository.getContent(test.mockContent.id) } returns mockGetContent(test)
        // Tests here...
        // Verification here...
    }
}

环境

  • MockK 版本:1.9.3
  • 操作系统:Mac 10.14.6
  • Kotlin 版本:1.3.50
  • JDK版本:12.0.1
  • JUnit 版本:5.5.1
  • 测试类型:单元测试
4

1 回答 1

1

这是MockK 创建者@oleksiy记录的这个GitHub 问题的错误。

一旦我看到错误得到解决,我将更新这篇文章。

于 2019-11-03T01:35:51.637 回答