Reputation: 18108
i am trying to mock some objects nd then inject them to a presenter but doesnt work
It sais the lateInt property that i have a mock on is not being initialised:
Here is my test clss
@Mock
lateinit var storage: StorageContract
@Mock
lateinit var activityCallBack: LaunchContract.ActivityCallBack
@InjectMocks
lateinit var launchPresenter: LaunchContract.Presenter
@get: Rule
@InjectMocks
var activity: ActivityTestRule<LaunchActivity> = ActivityTestRule<LaunchActivity>(LaunchActivity::class.java)
@Test
fun testRegAndLoginVisible() {
Mockito.`when`(storage.isLoggedIn()).thenReturn(false)
onView(withId(R.id.loginBtn))
.check(matches(isDisplayed()))
onView(withId(R.id.registerBtn))
.check(matches(isDisplayed()))
}
Here is my build script
testImplementation 'junit:junit:4.12'
testImplementation 'au.com.dius:pact-jvm-consumer-junit_2.11:3.5.10'
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation 'io.mockk:mockk:1.6.3'
testImplementation 'org.assertj:assertj-core:3.8.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.mockito:mockito-android:2.13.0'
The error i get:
kotlin.UninitializedPropertyAccessException: lateinit property storage has not been initialized
Upvotes: 1
Views: 2750
Reputation: 666
To have @Mock
annotations working correctly you should:
annotate your test class with @RunWith(MockitoJUnitRunner::class)
or
call MockitoAnnotations.initMocks(this)
in your "setup" method (annotated with @Before
)
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
}
EDIT:
Also, make sure you are using the latest version of JDK 8, Mockito has some issues with older JDK versions (https://github.com/mockito/mockito/issues/636)
Upvotes: 2