在Intellij中进行Android单元测试期间的日志记录给出了java.lang.RuntimeException:Stub

时间:2018-08-28 20:14:58

标签: android unit-testing intellij-idea

我目前正在尝试从Android Studio迁移到Intellij。

当我尝试通过单元测试运行代码时,就会出现问题。被测代码中的方法调用Log.e,并且在Intellij IDE中引发异常。相同的代码在Android Studio中运行良好。可能我的项目设置在Intellij中配置不正确。有人知道在哪里寻找问题吗?

java.lang.RuntimeException: Stub!
at android.util.Log.e(Log.java:31)

运行单元测试时我的Intellij命令。

D:\software\Java\jdk1.8.0_181\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 
"-javaagent:D:\software\IntelliJ IDEA Community Edition 2018.2.2\lib\idea_rt.jar=4006:D:\software\IntelliJ IDEA Community Edition 2019.2.2\bin" 
-Dfile.encoding=UTF-8 
-classpath 

"D:\software\IntelliJ IDEA Community Edition 2018.2.2\lib\idea_rt.jar;
 D:\software\IntelliJ IDEA Community Edition 2018.2.2\plugins\junit\lib\junit-rt.jar;
 D:\software\IntelliJ IDEA Community Edition 2018.2.2\plugins\junit\lib\junit5-rt.jar;
 D:\software\Android\sdk\platforms\android-28\android.jar;
 D:\software\Android\sdk\platforms\android-28\data\res;
 D:\software\Android\sdk\platforms\android-27\data\res;
 D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\classes\debug;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy-agent\1.7.9\a6c65f9da7f467ee1f02ff2841ffd3155aee2fc9\byte-buddy-agent-1.7.9.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\runner-1.0.2.aar\d44dda11e7de1711127415e572906e3c\jars\classes.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\2.6\639033469776fd37c08358c6b92a4761feb2af4b\objenesis-2.6.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.0\edf3f7bfb84a7521d0599efa3b0113a0ee90f85\common-1.1.0.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\27.1.1\39ded76b5e1ce1c5b2688e1d25cdc20ecee32007\support-annotations-27.1.1.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-27.1.1.aar\87b44b5092008154f1171a969696f102\jars\classes.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\jars\classes.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\res;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy\1.7.9\51218a01a882c04d0aba8c028179cce488bbcb58\byte-buddy-1.7.9.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.0.aar\8d37b7feb2744759cd91b03ad94efb30\jars\classes.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\2.15.0\b84bfbbc29cd22c9529409627af6ea2897f4fa85\mockito-core-2.15.0.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\com.nhaarman\mockito-kotlin\1.5.0\25faa884f76375f76cdbd6651c4cebcde36d4117\mockito-kotlin-1.5.0.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\com.squareup\javawriter\2.1.1\67ff45d9ae02e583d0f9b3432a5ebbe05c30c966\javawriter-2.1.1.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\com.github.kotlin-graphics\kotlin-unsigned\v2.1\52408e5d299c5d1fb669188dae56fa5bb37cbc12\kotlin-unsigned-v2.1.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-27.1.1.aar\8949c790c8f1bf421289f293aa4e1cc2\jars\classes.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\jars\classes.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\res;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-utils-27.1.1.aar\fb3bae07f3360874234d36f26caa5ebb\jars\classes.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\com.google.code.findbugs\jsr305\2.0.1\516c03b21d50a644d538de0f0369c620989cd8f0\jsr305-2.0.1.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\espresso-core-3.0.2.aar\369dec0c3f47cc8d75f961fddc94d0f7\jars\classes.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\javax.inject\javax.inject\1\6975da39a7040257bd51d21a231b76c915872d38\javax.inject-1.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-fragment-27.1.1.aar\0464588052e7fe27a8be9d866a5cd81f\jars\classes.jar;
 D:\software\Android\.gradle\caches\modul:es-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\4aa45f99c024430a3ccf7c0e257fb60a\jars\classes.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\monitor-1.0.2.aar\94d037806c32cc9e5663a80cfb787b67\jars\classes.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\res;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\jars\classes.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\espresso-idling-resource-3.0.2.aar\3b7ac85279e2aa734ba776a985e5273e\jars\classes.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-reflect\1.2.30\9758025a415ee400e1f90ff222bcfec247779133\kotlin-reflect-1.2.30.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.2.30\2dfac33f8b4e92c9dd1422cd286834701a6f6d6\kotlin-stdlib-1.2.30.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.0.aar\8c471aa13e8c6e412a4748be6e503cbc\jars\classes.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-library\1.3\4785a3c21320980282f9f33d0d1264a69040538f\hamcrest-library-1.3.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-integration\1.3\5de0c73fef18917cd85d0ab70bb23818685e4dfd\hamcrest-integration-1.3.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.0\8007981f7d7540d89cd18471b8e5dcd2b4f99167\common-1.1.0.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7\1.2.30\ca12c47fc1e3a7316067b2a51e2f214745ebf8c5\kotlin-stdlib-jdk7-1.2.30.jar;
 D:\software\Android\.gradle\caches\modules-2\files-2.1\net.sf.kxml\kxml2\2.3.0\ccbc77a5fd907ef863c29f3596c6f54ffa4e9442\kxml2-2.3.0.jar;
 D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\f827adbdf97ad86b4140dcb5e1073a50\jars\classes.jar;
 D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\sourceFolderJavaResources\test\debug;
 D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\unitTestConfig\test\debug;
 D:\code\android\MyProjectAndroid\myProjectandroidgc\build\tmp\kotlin-classes\debugUnitTest;
 D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\sourceFolderJavaResources\debug;
 D:\code\android\MyProjectAndroid\myProjectandroidgc\build\tmp\kotlin-classes\debug;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\generated\mockable-android-26.default-values.v3.jar"
 com.intellij.rt.execution.junit.JUnitStarter 
-ideVersion5 @w@C:\Users\cjf12\AppData\Local\Temp\idea_working_dirs_junit.tmp @C:\Users\cjf12\AppData\Local\Temp\idea_junit.tmp

我的Android Studio命令,用于运行单元测试。

"D:\software\Android\Android Studio\jre\bin\java"
-ea
-Didea.test.cyclic.buffer.size=1048576
-Didea.launcher.port=3647
"-Didea.launcher.bin.path=D:\software\Android\Android Studio\bin"
-Dfile.encoding=UTF-8
-classpath
     "D:\software\Android\Android Studio\lib\idea_rt.jar;
     D:\software\Android\Android Studio\plugins\junit\lib\junit-rt.jar;
     D:\software\Android\Android Studio\plugins\junit\lib\junit5-rt.jar;
     D:\software\Android\sdk\platforms\android-27\data\res;
     D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\classes\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\tmp\kotlin-classes\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\rs\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\resValues\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\unitTestConfig\test\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\tmp\kotlin-classes\debugUnitTest;
     D:\code\android\myProjectAndroid\myprojectandroid\build\tmp\kotlin-classes\debugAndroidTest;
     D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\rs\androidTest\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\resValues\androidTest\debug;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy-agent\1.7.9\a6c65f9da7f467ee1f02ff2841ffd3155aee2fc9\byte-buddy-agent-1.7.9.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\2.6\639033469776fd37c08358c6b92a4761feb2af4b\objenesis-2.6.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.0\edf3f7bfb84a7521d0599efa3b0113a0ee90f85\common-1.1.0.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\27.1.1\39ded76b5e1ce1c5b2688e1d25cdc20ecee32007\support-annotations-27.1.1.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-27.1.1.aar\87b44b5092008154f1171a969696f102\jars\classes.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\jars\classes.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\res;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy\1.7.9\51218a01a882c04d0aba8c028179cce488bbcb58\byte-buddy-1.7.9.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.0.aar\8d37b7feb2744759cd91b03ad94efb30\jars\classes.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\2.15.0\b84bfbbc29cd22c9529409627af6ea2897f4fa85\mockito-core-2.15.0.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\com.nhaarman\mockito-kotlin\1.5.0\25faa884f76375f76cdbd6651c4cebcde36d4117\mockito-kotlin-1.5.0.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\com.github.kotlin-graphics\kotlin-unsigned\v2.1\52408e5d299c5d1fb669188dae56fa5bb37cbc12\kotlin-unsigned-v2.1.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-27.1.1.aar\8949c790c8f1bf421289f293aa4e1cc2\jars\classes.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\jars\classes.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\res;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-utils-27.1.1.aar\fb3bae07f3360874234d36f26caa5ebb\jars\classes.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-fragment-27.1.1.aar\0464588052e7fe27a8be9d866a5cd81f\jars\classes.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\4aa45f99c024430a3ccf7c0e257fb60a\jars\classes.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\res;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\jars\classes.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-reflect\1.2.30\9758025a415ee400e1f90ff222bcfec247779133\kotlin-reflect-1.2.30.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.2.30\2dfac33f8b4e92c9dd1422cd286834701a6f6d6\kotlin-stdlib-1.2.30.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.0.aar\8c471aa13e8c6e412a4748be6e503cbc\jars\classes.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.0\8007981f7d7540d89cd18471b8e5dcd2b4f99167\common-1.1.0.jar;
     D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7\1.2.30\ca12c47fc1e3a7316067b2a51e2f214745ebf8c5\kotlin-stdlib-jdk7-1.2.30.jar;
     D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\f827adbdf97ad86b4140dcb5e1073a50\jars\classes.jar;
     D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\sourceFolderJavaResources\test\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\sourceFolderJavaResources\debug;
     D:\code\android\myProjectAndroid\myprojectandroid\build\generated\mockable-android-27.default-values.v3.jar"
com.intellij.rt.execution.application.AppMainV2
com.intellij.rt.execution.junit.JUnitStarter
-ideVersion5
@w@C:\Users\cjf12\AppData\Local\Temp\idea_working_dirs_junit.tmp
@C:\Users\cjf12\AppData\Local\Temp\idea_junit.tmp
-socket3646

这是我的build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "org.mycompany.myproject"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        main.java.srcDirs += 'src/main/java'
        test.java.srcDirs += 'src/test/java'
    }

    buildTypes {
        debug {
            testCoverageEnabled true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = true
        }
    }
}

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.github.kotlin-graphics:kotlin-unsigned:v2.1'

    // Required -- JUnit 4 framework
    testImplementation 'junit:junit:4.12'
    // Optional -- Mockito framework
    testImplementation 'org.mockito:mockito-core:2.15.0'
    testImplementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
    testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    testImplementation 'com.github.kotlin-graphics:kotlin-unsigned:v2.1'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

2 个答案:

答案 0 :(得分:2)

1-将其添加到您的build.gradle

testImplementation "org.mockito:mockito-core:${mockitoVersion}"
testImplementation "org.powermock:powermock-api-mockito:${powerMockVersion}"

Checkout the supported versions of Mockito and PowerMock

例如,如果您将v1.9.5用于Mockito,则将v1.6.6用于PowerMock

2-用此注释您的测试班

@RunWith(PowerMockRunner.class)
@PrepareForTesting(Log.class)
public class YourTestClass {

您的测试用例现在应该运行。

@ Code-Apprentice也说过,如果您想在测试类中记录某些内容,请使用System.out.println()。不要使用Android依赖项进行测试,这只是绕过您正在进行单元测试的方法中的日志。

更新

将此内容添加到您的build.gradle也将帮助您不需要PowerMock(强烈建议不要这样做)。请参阅this answer

android { 
     //... 
     testOptions { 
          unitTests.returnDefaultValues = true 
     }
 }

答案 1 :(得分:0)

运行单元测试时,Android API中没有可用的方法。错误“ java.lang.RuntimeException:存根!”当您尝试调用任何这些函数(包括Log.e())时,都会发生这种情况。如果需要输出,请改用标准System.out.println()。或使用IntelliJ调试器。