MVP Mockito 單元測試
引用內容:
//測試庫
// Dependencies for local unit tests
testImplementation deps.junit
testCompile deps.mockito.core
testImplementation deps.hamcrest
// Android Testing Support Library’s runner and rules
androidTestImplementation deps.atsl.runner
androidTestImplementation deps.atsl.rules
// Dependencies for Android unit tests
androidTestImplementation deps.junit
androidTestCompile deps.mockito.android
androidTestCompile deps.mockito.core
// Espresso UI Testing
androidTestImplementation deps.espresso.core
androidTestImplementation deps.espresso.contrib
androidTestImplementation deps.espresso.intents
androidTestImplementation deps.espresso.idling
implementation deps.espresso.idling_resource
// Resolve conflicts between main and test APK:
androidTestImplementation deps.support.annotations
androidTestImplementation deps.support.v4
androidTestImplementation deps.support.recyclerview
androidTestImplementation deps.support.app_compat
androidTestImplementation deps.support.design
下面是測試程式碼
@Mock
private lateinit var mView: MaterialManagermentClassifyContract.View
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
// The presenter wont't update the view unless it's active.
`when`(mView.isActive()).thenReturn(true)
}
注意Contract.View中不可以有fragment的引用,要不會報錯,這個問題折騰了一天
Mocking final types, enums and final methods
新增/mockito-extensions/org.mockito.plugins.MockMaker
並且內容為mock-maker-inline
方式 final 方法報錯