如何在Android中使用Dagger进行Kotlin文件的UI测试? [英] How to do UI testing for kotlin file with dagger in android?

查看:143
本文介绍了如何在Android中使用Dagger进行Kotlin文件的UI测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的堆栈跟踪,我已经了解了SO上的所有问题和答案,但是找不到任何解决方案

Below is my stack trace, I have gone through all the questions and answers on SO but can't find any solution

java.lang.IllegalStateException: Could not initialize plugin: interface 
org.mockito.plugins.MockMaker (alternate: null)
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy6.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:232)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:64)
at org.mockito.Mockito.mock(Mockito.java:1871)
at org.mockito.Mockito.mock(Mockito.java:1780)
at SplashActivityTest.init(SplashActivityTest.kt:126)
at java.lang.reflect.Method.invoke(Native Method) 
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lnet/bytebuddy/dynamic/loading/ClassInjector$UsingReflection;
at org.mockito.internal.creation.bytebuddy.SubclassInjectionLoader.<init>(SubclassInjectionLoader.java:28)
at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.<init>(SubclassByteBuddyMockMaker.java:33)
at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.<init>(ByteBuddyMockMaker.java:21)
at java.lang.Class.newInstance(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/com.test-Ceb6_iDz-8wl1a3HhgqEEg==/base.apk", zip file "/data/app/YwRi3yxfA1u5ckInmXjV-A==/base.apk"],nativeLibraryDirectories=[/data/app/test-Ceb6_iDz-8wl1a3HhgqEEg==/lib/x86, /data/app/YwRi3yxfA1u5ckInmXjV-A==/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)

下面是我的splashActivityTest

Below is my splashActivityTest,

import android.view.View
import android.view.ViewGroup
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnit4
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsInstanceOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@LargeTest
@RunWith(AndroidJUnit4::class)
class SplashActivityTest {

@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(SplashActivity::class.java)
@Rule
@JvmField
val executorRule = TaskExecutorWithIdlingResourceRule()
@Rule
@JvmField
val countingAppExecutors = CountingAppExecutorsRule()
@Rule
@JvmField
val dataBindingIdlingResourceRule = DataBindingIdlingResourceRule(mActivityTestRule)

private lateinit var prefUtils: PrefUtils
private lateinit var navigationController: NavigationController

@Before
fun init() {
    prefUtils = mock()
    navigationController = mock()
}

@Test
fun splashActivityTest() {
    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    Thread.sleep(2000)

    val imageView = onView(
        allOf(
            withId(R.id.logo),
            childAtPosition(
                childAtPosition(
                    IsInstanceOf.instanceOf(android.widget.FrameLayout::class.java),
                    0
                ),
                0
            ),
            isDisplayed()
        )
    )
    imageView.check(matches(isDisplayed()))

    val frameLayout = onView(
        allOf(
            childAtPosition(
                childAtPosition(
                    withId(android.R.id.content),
                    0
                ),
                0
            ),
            isDisplayed()
        )
    )
    frameLayout.check(matches(isDisplayed()))

    val frameLayout2 = onView(
        allOf(
            childAtPosition(
                childAtPosition(
                    withId(android.R.id.content),
                    0
                ),
                0
            ),
            isDisplayed()
        )
    )
    frameLayout2.check(matches(isDisplayed()))
}

private fun childAtPosition(
    parentMatcher: Matcher<View>, position: Int
): Matcher<View> {

    return object : TypeSafeMatcher<View>() {
        override fun describeTo(description: Description) {
            description.appendText("Child at position $position in parent ")
            parentMatcher.describeTo(description)
        }

        public override fun matchesSafely(view: View): Boolean {
            val parent = view.parent
            return parent is ViewGroup && parentMatcher.matches(parent)
                    && view == parent.getChildAt(position)
        }
    }
  }
}

实际SplashActivity

Actual SplashActivity

@OpenForTesting
class SplashActivity : BaseActivity() {

/**
 * Returns layout file ID
 * */
override fun layoutId() = R.layout.activity_splash

/**
 * this method gets called when this activity gets created
 * all tasks those need to be executed when this activity get created
 * */
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    /**
     * TO Load Gif in #ImageView
     */
    Glide.with(this).load(R.raw.new_loading_logo).into(logo)
    /**
     * Handles Timer of 2000 millSeconds to open another Activity
     * prefUtils.isUserLogin() == true -> Opens DashBoard as User is already Logged In
     * else -> Opens Log In Page
     */
    //*while UI test runs, Here when prefUtils.isUserLogin() gets executed See error log below*
    Handler().postDelayed({
        if (prefUtils.isUserLogin())
            navigationController.navigateToDashBoard(this)
        else
            navigationController.navigateToLogin(this)
        finish()
    }, 2000)
  }
}

此Activity扩展了具有下面几行的BaseActivity,因此启动启动后,我的测试失败了,并且未初始化lateinit var prefUtils的统计信息,现在为此我使用了模拟功能,但仍得到java.lang.IllegalStateException:无法初始化插件:接口 org.mockito.plugins.MockMaker(备用:null).

This Activity extends BaseActivity which is having below lines, hence after launch of splash, my test goes failed and stats that lateinit var prefUtils has not been initialised, now for this i have used mocking but still getting java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null).

@Inject
lateinit var navigationController: NavigationController
@Inject
lateinit var prefUtils: PrefUtils
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory

我添加了以下依赖项

testImplementation "junit:junit:$junitVersion"
testImplementation "org.mockito:mockito-core:$mockito"
testImplementation "org.mockito:mockito-inline:$mockito"

当我不嘲笑任何东西时,发生以下错误

Below error happens when i dont mock anything,

kotlin.UninitializedPropertyAccessException: lateinit property prefUtils has not been initialized
at BaseActivity.getPrefUtils(BaseActivity.kt:41)
at SplashActivity$onCreate$1.run(SplashActivity.kt:38)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Test running failed: Instrumentation run failed due to 'Process crashed.'

因为baseActivity在下面 @注入 lateinit var prefUtils:PrefUtils 因此,为了解决此错误,我使用了Mock

as baseActivity has below line @Inject lateinit var prefUtils: PrefUtils Hence for solving this error i am using Mock

现在我也尝试过功率模拟,但是它在编译时失败了!,它指出

Now I have tried power mock too but it failed on compile time !!, It states

Unresolved reference: powermock

在行下面

@RunWith(PowerMockRunner::class)

推荐答案

此处SplashActivity继承了BaseActivity,因此无论何时运行测试,注入的变量仍未初始化以进行测试,因此我通过更改下面的行找到了解决方法

Here SplashActivity is inheriting BaseActivity hence whenever test runs, Injected variables remains uninitialized for test hence I found solution by changing below line

@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(SplashActivity::class.java)

@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(FakeLogInActivity::class.java)

下面是FakeLogInActivity :: class.java的代码

Below is code for FakeLogInActivity::class.java

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.sextpanther.sp.R

/**
* Used for testing fragments inside a fake activity.
*/
class FakeLogInActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
  }
}

现在测试已成功运行,但仅对带有文本的快餐栏视图匹配器进行的测试失败.

Now test running successfully, but only test with snackbar view matcher with text gets failed.

这篇关于如何在Android中使用Dagger进行Kotlin文件的UI测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆