小编Mar*_*. F的帖子

如何使用 Mockito 模拟意图?

我试图在我所做的函数中模拟意图。这是下面的函数

fun learningUnitTest(context: Context) {
    val str = context.getString(R.string.app_name)
    val intent = Intent(context, TodoFragment::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    } // it resulting null. how to mock this?
    val pendingIntent: PendingIntent = PendingIntent.getActivity(context, 0, intent, 0)

    Log.d("AA", str)
    Log.d("BB", pendingIntent.toString())
}
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试文件

import android.content.Context
import android.content.Intent
import android.util.Log
import com.example.todoapp.controller.worker.learningUnitTest
import io.mockk.*
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.*

class ExampleUnitTest {

    private val FAKE_STRING = "FAKE"

    @Mock
    private lateinit var mockContext: Context

    @Test
    fun test_function() {
        mockkStatic(Log::class)
        mockkStatic(Intent::class)
        mockContext …
Run Code Online (Sandbox Code Playgroud)

junit mockito android-intent android-studio mockk

5
推荐指数
0
解决办法
1351
查看次数

标签 统计

android-intent ×1

android-studio ×1

junit ×1

mockito ×1

mockk ×1