Android协同程序MVP junit测试代码失败
我正在使用Kotlin(Coroutine)开发一个玩具Android应用程序 我更熟悉Rx,而不是协同程序 我觉得合作比Rx更难 无论如何,以下是我的演示者代码:Android协同程序MVP junit测试代码失败,android,kotlin,junit,coroutine,clean-architecture,Android,Kotlin,Junit,Coroutine,Clean Architecture,我正在使用Kotlin(Coroutine)开发一个玩具Android应用程序 我更熟悉Rx,而不是协同程序 我觉得合作比Rx更难 无论如何,以下是我的演示者代码: class NowPlayingPresenter( private val view: NowPlayingContract.View, private val getMovies: GetNowPlayingMovies, private val uiContext: CoroutineContext =
class NowPlayingPresenter(
private val view: NowPlayingContract.View,
private val getMovies: GetNowPlayingMovies,
private val uiContext: CoroutineContext = Dispatchers.Main,
ioContext: CoroutineContext = Dispatchers.IO
) : NowPlayingContract.Presenter, CoroutineScope, AnkoLogger {
override val coroutineContext: CoroutineContext = Job() + ioContext
override fun unsubscribe() {
coroutineContext.cancel()
}
override fun getMoviesNowPlaying() {
view.showProgressBar(View.VISIBLE)
view.hideError()
launch {
try {
val movies = getMovies.get()
// 'movies' is always null!!! I don't know why...
withContext(uiContext) {
view.showProgressBar(View.GONE)
if (movies.isNullOrEmpty()) {
view.onError(R.string.err_movies_not_exists)
} else {
view.onMoviesLoaded(movies)
}
}
} catch (t: Throwable) {
view.showProgressBar(View.GONE)
view.onError(R.string.err_get_movies_failed)
error("[Y.M.] getMoviesNowPlaying - failed: ${t.message}", t)
}
}
}
}
这是我的GetNowPlayingMovies代码,它只是一个界面:
interface GetNowPlayingMovies {
suspend fun get(): List<SimpleMovie>
}
界面GetNowPlayingMovies{
suspend fun get():列表
}
下面是我的JUnit测试代码:
class MyDataPresenterTest {
@Mock
private lateinit var mockView: NowPlayingContract.View
@Mock
private lateinit var getMovies: GetNowPlayingMovies
private lateinit var presenter: NowPlayingPresenter
private lateinit var inOrder: InOrder
private val mockMovie1 = SimpleMovie("posterpath1", false, "2019-03-01", 10, "hello world1", 10f)
private val mockMovie2 = SimpleMovie("posterpath2", true, "2019-03-02", 20, "hello world2", 9f)
private val mockMovie3 = SimpleMovie("posterpath3", false, "2019-03-03", 30, "hello world3", 8f)
private val mockMovie4 = SimpleMovie("posterpath4", false, "2019-03-04", 40, "hello world4", 7f)
private val mockMovie5 = SimpleMovie("posterpath5", false, "2019-03-05", 50, "hello world5", 6f)
private val mockMovies: List<SimpleMovie> = listOf(
mockMovie1,
mockMovie2,
mockMovie3,
mockMovie4,
mockMovie5
)
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
inOrder = Mockito.inOrder(mockView)
presenter = NowPlayingPresenter(mockView, getMovies, Dispatchers.Unconfined, Dispatchers.Unconfined)
}
@Test
fun getMoviesNowPlayingTest() = runBlocking {
`when`(getMovies.get()).thenReturn(mockMovies)
presenter.getMoviesNowPlaying()
inOrder.verify(mockView).showProgressBar(View.VISIBLE)
inOrder.verify(mockView).hideError()
inOrder.verify(mockView).showProgressBar(View.GONE)
inOrder.verify(mockView).onMoviesLoaded(mockMovies)
}
}
类MyDataPresenterTest{
@嘲弄
私有lateinit var mockView:NowPlayingContract.View
@嘲弄
私有lateinit var getMovies:GetNowPlayingMovies
私有lateinit var演示者:NowPlayingPresenter
私有lateinit变量索引顺序:索引顺序
private val mockMovie1=SimpleMovie(“posterpath1”,false,“2019-03-01”,10,“hello world1”,10f)
private val mockMovie2=SimpleMovie(“posterpath2”,真实,“2019-03-02”,20,“hello world2”,9f)
private val mockMovie3=SimpleMovie(“posterpath3”,false,“2019-03-03”,30,“hello world3”,8f)
private val mockMovie4=SimpleMovie(“posterpath4”,false,“2019-03-04”,40,“hello world4”,7f)
private val mockMovie5=SimpleMovie(“posterpath5”,false,“2019-03-05”,50,“hello world5”,6f)
private val mockmockmockmovies:List=listOf(
模拟电影1,
模拟电影2,
模拟电影3,
模拟电影4,
模拟电影5
)
@以前
趣味设置(){
initMocks(this)
inoorder=Mockito.inoorder(mockView)
presenter=NowPlayingPresenter(mockView、getMovies、Dispatchers.Unconfined、Dispatchers.Unconfined)
}
@试验
fun getMoviesNowPlayingTest()=运行阻塞{
`当`(getMovies.get())。然后返回(mockMovies)
presenter.getMoviesNowPlaying()
inOrder.verify(mockView.showProgressBar)(View.VISIBLE)
inOrder.verify(mockView.hideError())
inOrder.verify(mockView.showProgressBar)(View.GONE)
inOrder.verify(mockView).onMoviesLoaded(mockMovies)
}
}
谁来帮帮我
这是我的全部代码:
我试图在这个项目中应用“干净的体系结构”,所以代码不容易阅读。。。也许…就我的情况和惯例而言
发布{
val data=withContext(ioContext){
repository.get(id)
}
withContext(uiContext){
视图?.onDataLoaded(数据)
}
}
您正在使用Mockito 1.x。如果您使用mock for suspend函数,请使用最新的Mockito 如果你发布创建mockData
对象代码,这将非常有用。你能分享失败测试的消息吗?r2rek,我已经在演示者代码中写了错误消息作为注释。Ircover,它只是简单的数据类列表,当我在测试代码中选中它时,它不是空的。我应用了你的代码,但它失败了,错误消息为:线程“main@coroutine#1”java.lang.IllegalStateException中的异常:具有主调度程序的模块未能初始化。对于测试,可使用kotlinx coroutines测试模块中的Dispatchers.setMain