Reputation: 10364
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
.
Warning message/Build fail:
Repeatable annotations with non-SOURCE retention are not yet supported.
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...
}
}
Upvotes: 0
Views: 639
Reputation: 10364
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.
Upvotes: 1