JUnit 5注释-不适用于LiveData /参数化测试

问题描述 投票:0回答:1

概述

预期行为

使用mockObject函数implementation替换模拟对象初始化,并用documentationMedium post概述了JUnit 5的注释语法初始化。

当前行为

所讨论的测试是针对JUnit 5的@oleksiyp所概述的parameterized test,似乎与@phauer冲突。为了使用LiveData进行测试,测试必须使用@ExtendWith(MockKExtension::class)设计的InstantExecutorExtension在本地单元测试中同步运行。

模拟对象初始化可以与InstantExecutorExtension函数一起正常工作,但是无法使用注释@JeroenMols

错误

警告消息/构建失败:

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

实施

mockObject函数实现(按预期工作)

@MockK

Annotation语法初始化(由于两个扩展名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...
    }
}

环境

  • MockK版本:1.9.3
  • OS:Mac 10.14.6
  • Kotlin版本:1.3.50
  • JDK版本:12.0.1
  • JUnit版本:5.5.1
  • 测试类型:单元测试
android kotlin android-testing junit5 mockk
1个回答
0
投票

根据MockK创建者@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... } } 的错误。

© www.soinside.com 2019 - 2024. All rights reserved.