0

Overview

Expected Behavior

Replace mock object initialization using the mockObject function implementation with annotation syntax initialization for JUnit 5 as outlined in the documentation and Medium post by @oleksiyp.

Current Behavior

The test in question is a parameterized test as outlined by @phauer for JUnit 5 which seems to conflict with @ExtendWith(MockKExtension::class). In order to implement tests with LiveData the test must run synchronously in the local unit test using this InstantExecutorExtension designed by @JeroenMols.

Mock object initialization works as expected with the mockObject function, but fails using the annotation @MockK.

Error

Warning message/Build fail:

Repeatable annotations with non-SOURCE retention are not yet supported.

Implementation

mockObject function implementation (Working as expected)

@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...
    }
}

Annotation syntax initialization (Not working due to two extensions @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...
    }
}

Environment

  • MockK version: 1.9.3
  • OS: Mac 10.14.6
  • Kotlin version: 1.3.50
  • JDK version: 12.0.1
  • JUnit version: 5.5.1
  • Type of test: Unit test
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
  • Try to combine @ExtendsWith.. @ExtendWith(InstantExecutorExtension::class) @ExtendWith(MockKExtension::class) Into combined way: **@ExtendWith({InstantExecutorExtension::class, MockKExtension::class})** – mmmatey Oct 17 '19 at 08:24

1 Answers1

1

This is a bug according to this GitHub issue, as documented by the MockK creator, @oleksiy.

I will update this post once I see the bug is resolved.

AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134