getExternalStorageState in android.os.Environment not mocked

Ping_E_Lee
  • 17

使用kotlin代码编写一个测试类
build.gradle配置如下

plugins {
  id 'com.android.application'
  id 'kotlin-android'
}

android {
  compileSdkVersion 31
  buildToolsVersion '30.0.3'

  defaultConfig {
    applicationId "com.litongjava.criminalintent"
    minSdkVersion 16
    targetSdkVersion 31
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  kotlinOptions {
    jvmTarget = '1.8'
  }
}

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation 'androidx.core:core-ktx:1.2.0'
  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'com.google.android.material:material:1.1.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  implementation 'com.android.support:appcompat-v7:28.0.0'
  testImplementation 'junit:junit:4.+'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

  //noinspection GradleCompatible
  implementation 'androidx.recyclerview:recyclerview:1.0.0'
  implementation 'androidx.legacy:legacy-support-v4:1.0.0'
  implementation 'org.slf4j:slf4j-api:1.7.25'
  implementation 'com.github.tony19:logback-android:2.0.0'

}

编写一个测试类

package com.litongjava.criminalintent

import android.text.format.DateFormat
import org.junit.Test
import org.slf4j.LoggerFactory
import java.util.*

class DateFormatTest {
  private val log=LoggerFactory.getLogger(this::class.java)
  @Test
  fun formatTest(){
    var date=Date()
    val formatPattern = "EEEE, MMMM dd, yyyy" //星期,月份 几号,几年   例如:星期一,八月 24, 2015
    var formatString = DateFormat.format(formatPattern,date)
    log.info("initDateButton: format:${formatString}") //->Wednesday, September 08, 2021
  }
}

运行测试类出现下面的错误

D:\dev_program\java\jdk-11.0.8\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\Android\Android Studio\lib\idea_rt.jar=1270:D:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Android\Android Studio\lib\idea_rt.jar;D:\Program Files\Android\Android Studio\plugins\junit\lib\junit5-rt.jar;D:\Program Files\Android\Android Studio\plugins\junit\lib\junit-rt.jar;D:\Android\Sdk\platforms\android-31\data\res;E:\dev_workspace\android\android-ping\CriminalIntent\app\build\intermediates\javac\debug\classes;E:\dev_workspace\android\android-ping\CriminalIntent\app\build\tmp\kotlin-classes\debugUnitTest;E:\dev_workspace\android\android-ping\CriminalIntent\app\build\intermediates\compile_and_runtime_not_namespaced_r_class_jar\debug\R.jar;E:\dev_workspace\android\android-ping\CriminalIntent\app\build\tmp\kotlin-classes\debug;E:\dev_workspace\android\android-ping\CriminalIntent\app\build\generated\res\resValues\debug;D:\dev_mavenRepository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\33256b64085295e5ceea6e624df60830\jetified-junit-1.1.1\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\90ff4e97417967428ddc72693cd00933\espresso-core-3.2.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\56527380ff47613096e1a510d289aa6a\runner-1.2.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\f5a2c73c1fba8c734c6d287500e74009\jetified-core-1.2.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\40c1e2040b0baaced6037b23d9cb61d0\monitor-1.2.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\719587a541ce45b6543adc0178981cf8\espresso-idling-resource-3.2.0\AndroidManifest.xml;D:\dev_mavenRepository\junit\junit\4.13.2\junit-4.13.2.jar;D:\dev_gradleRespository\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.4.32\461367948840adbb0839c51d91ed74ef4a9ccb52\kotlin-stdlib-1.4.32.jar;D:\dev_gradleRespository\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-common\1.4.32\ef50bfa2c0491a11dcc35d9822edbfd6170e1ea2\kotlin-stdlib-common-1.4.32.jar;D:\dev_mavenRepository\org\jetbrains\annotations\13.0\annotations-13.0.jar;D:\dev_gradleRespository\caches\modules-2\files-2.1\androidx.collection\collection\1.1.0\1f27220b47669781457de0d600849a5de0e89909\collection-1.1.0.jar;D:\dev_gradleRespository\caches\modules-2\files-2.1\androidx.lifecycle\lifecycle-common\2.1.0\c67e7807d9cd6c329b9d0218b2ec4e505dd340b7\lifecycle-common-2.1.0.jar;D:\dev_gradleRespository\caches\modules-2\files-2.1\androidx.arch.core\core-common\2.1.0\b3152fc64428c9354344bd89848ecddc09b6f07e\core-common-2.1.0.jar;D:\dev_gradleRespository\caches\modules-2\files-2.1\androidx.annotation\annotation\1.1.0\e3a6fb2f40e3a3842e6b7472628ba4ce416ea4c8\annotation-1.1.0.jar;D:\dev_gradleRespository\caches\modules-2\files-2.1\androidx.constraintlayout\constraintlayout-solver\1.1.3\54abe9ffb22cc9019b0b6fcc10f185cc4e67b34e\constraintlayout-solver-1.1.3.jar;D:\dev_mavenRepository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\056692af005eb43a81b768e67cda6f87\jetified-core-ktx-1.2.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\056692af005eb43a81b768e67cda6f87\jetified-core-ktx-1.2.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\0048295f936f7f8e840fd4a81778e6c3\material-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\0048295f936f7f8e840fd4a81778e6c3\material-1.1.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\0048295f936f7f8e840fd4a81778e6c3\material-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\1bab688942677ff64146c06c56445fcd\appcompat-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\1bab688942677ff64146c06c56445fcd\appcompat-1.1.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\1bab688942677ff64146c06c56445fcd\appcompat-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\a29cc0fc55eab8801f86bbf5211d8ec3\jetified-viewpager2-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\a29cc0fc55eab8801f86bbf5211d8ec3\jetified-viewpager2-1.0.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\a29cc0fc55eab8801f86bbf5211d8ec3\jetified-viewpager2-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3d10205c2e56b950b8bc7aaf2eaa3c38\recyclerview-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3d10205c2e56b950b8bc7aaf2eaa3c38\recyclerview-1.1.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3d10205c2e56b950b8bc7aaf2eaa3c38\recyclerview-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\8441f0b9ef4e0575049c15475a19c3cc\legacy-support-v4-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\8441f0b9ef4e0575049c15475a19c3cc\legacy-support-v4-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\8002d60bf919252995b27d17131d36aa\fragment-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\8002d60bf919252995b27d17131d36aa\fragment-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\f0cd8b6ba4aa3e0c638fa9fb8f73dea8\jetified-appcompat-resources-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\f0cd8b6ba4aa3e0c638fa9fb8f73dea8\jetified-appcompat-resources-1.1.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\f0cd8b6ba4aa3e0c638fa9fb8f73dea8\jetified-appcompat-resources-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\d4d98051f9b78e03ff9337dc2c12a3ad\legacy-support-core-ui-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\d4d98051f9b78e03ff9337dc2c12a3ad\legacy-support-core-ui-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\d8f137a5aaec9aee031348d98c86a263\drawerlayout-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\d8f137a5aaec9aee031348d98c86a263\drawerlayout-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3407a73221adf6c51cee3f520aa4a99c\coordinatorlayout-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3407a73221adf6c51cee3f520aa4a99c\coordinatorlayout-1.1.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3407a73221adf6c51cee3f520aa4a99c\coordinatorlayout-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\48a74914606bb9b354ea95e8fb46372c\transition-1.2.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\48a74914606bb9b354ea95e8fb46372c\transition-1.2.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\48a74914606bb9b354ea95e8fb46372c\transition-1.2.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\5969ca2e3b2a44dce36474c3eb6e2c0f\vectordrawable-animated-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\5969ca2e3b2a44dce36474c3eb6e2c0f\vectordrawable-animated-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\98bf9f26db8d3fa4f1d5eb4bcd998e39\vectordrawable-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\98bf9f26db8d3fa4f1d5eb4bcd998e39\vectordrawable-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\2c128e7602d6bb34e9a85ebe44318369\viewpager-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\2c128e7602d6bb34e9a85ebe44318369\viewpager-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\b252122df95572a486f5664d952791c5\slidingpanelayout-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\b252122df95572a486f5664d952791c5\slidingpanelayout-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\a19ff9ce22cf45de56e0d74e5842eaf3\customview-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\a19ff9ce22cf45de56e0d74e5842eaf3\customview-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\86abdb25982e3642906d3c1b3b2f44b5\media-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\86abdb25982e3642906d3c1b3b2f44b5\media-1.0.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\86abdb25982e3642906d3c1b3b2f44b5\media-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\88a50cd71e56ce8d250b42e98d65343f\legacy-support-core-utils-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\88a50cd71e56ce8d250b42e98d65343f\legacy-support-core-utils-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\b2015d663f7153641c4a535ba3e220fc\loader-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\b2015d663f7153641c4a535ba3e220fc\loader-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\e56971afeb02dce344236eed0cd52ffe\jetified-activity-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\e56971afeb02dce344236eed0cd52ffe\jetified-activity-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\a5a0b063ee27ba656c86b2b7494c9ab9\swiperefreshlayout-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\a5a0b063ee27ba656c86b2b7494c9ab9\swiperefreshlayout-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\315b26dadae84d31568a2fda0ac1636f\asynclayoutinflater-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\315b26dadae84d31568a2fda0ac1636f\asynclayoutinflater-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\ad984d97a6f16dadd2a562f78a6b465b\core-1.2.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\ad984d97a6f16dadd2a562f78a6b465b\core-1.2.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\ad984d97a6f16dadd2a562f78a6b465b\core-1.2.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\b829576802764278d934e69c98584a66\cursoradapter-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\b829576802764278d934e69c98584a66\cursoradapter-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\bf9232b0ae0799f4d144928ab679e4bd\versionedparcelable-1.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\bf9232b0ae0799f4d144928ab679e4bd\versionedparcelable-1.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\d92628789da29d3dbfa2496b8e4145c1\cardview-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\d92628789da29d3dbfa2496b8e4145c1\cardview-1.0.0\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\d92628789da29d3dbfa2496b8e4145c1\cardview-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\4cc76781d97bc4a301579728641c7575\lifecycle-runtime-2.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\4cc76781d97bc4a301579728641c7575\lifecycle-runtime-2.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\538dac4564588a0aa5e1abe91326c2e3\lifecycle-viewmodel-2.1.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\538dac4564588a0aa5e1abe91326c2e3\lifecycle-viewmodel-2.1.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\554a3bcb675f3414c23b17dfbb898ebf\jetified-savedstate-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\554a3bcb675f3414c23b17dfbb898ebf\jetified-savedstate-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\19102a935862607a2ba809ba34d06223\lifecycle-livedata-2.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\19102a935862607a2ba809ba34d06223\lifecycle-livedata-2.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\8f303fbec44800507428cf5f57b2b5b8\lifecycle-livedata-core-2.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\8f303fbec44800507428cf5f57b2b5b8\lifecycle-livedata-core-2.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\c469d6ed97143be8863bcf7fbf18c983\core-runtime-2.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\c469d6ed97143be8863bcf7fbf18c983\core-runtime-2.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\e1cf46b678f422a5d99c23cbf702dc88\documentfile-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\e1cf46b678f422a5d99c23cbf702dc88\documentfile-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\7e137eeb45a71a6dc4c4288c51dfa86f\localbroadcastmanager-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\7e137eeb45a71a6dc4c4288c51dfa86f\localbroadcastmanager-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3fa017593e49f4b71cca3dea4b315356\print-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\3fa017593e49f4b71cca3dea4b315356\print-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\efbbe0368b31fe1ba8487b240152aef4\interpolator-1.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\efbbe0368b31fe1ba8487b240152aef4\interpolator-1.0.0\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\e74f6ebf6fb228831556c16a5f8510e8\constraintlayout-1.1.3\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\e74f6ebf6fb228831556c16a5f8510e8\constraintlayout-1.1.3\res;D:\dev_gradleRespository\caches\transforms-2\files-2.1\e74f6ebf6fb228831556c16a5f8510e8\constraintlayout-1.1.3\AndroidManifest.xml;D:\dev_gradleRespository\caches\transforms-2\files-2.1\c776bbaa0c077b19c14e8dd61159cdad\jetified-logback-android-2.0.0\jars\classes.jar;D:\dev_gradleRespository\caches\transforms-2\files-2.1\c776bbaa0c077b19c14e8dd61159cdad\jetified-logback-android-2.0.0\AndroidManifest.xml;E:\dev_workspace\android\android-ping\CriminalIntent\app\build\intermediates\java_res\debugUnitTest\out;E:\dev_workspace\android\android-ping\CriminalIntent\app\build\intermediates\java_res\debug\out;D:\dev_gradleRespository\caches\transforms-2\files-2.1\7f1d714e7ab31a8a14bb825306ab2508\android.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.litongjava.criminalintent.DateFormatTest,formatTest
Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.RuntimeException: Method getExternalStorageState in android.os.Environment not mocked. See http://g.co/androidstudio/not-mocked for details.
    at android.os.Environment.getExternalStorageState(Environment.java)
    at ch.qos.logback.core.android.AndroidContextUtil.getMountedExternalStorageDirectoryPath(Unknown Source)
    at ch.qos.logback.core.android.AndroidContextUtil.setupProperties(Unknown Source)
    at ch.qos.logback.classic.util.ContextInitializer.autoConfig(Unknown Source)
    at org.slf4j.impl.StaticLoggerBinder.init(Unknown Source)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(Unknown Source)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at com.litongjava.criminalintent.DateFormatTest.<init>(DateFormatTest.kt:10)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:250)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:260)
    at org.junit.runners.BlockJUnit4ClassRunner$2.runReflectiveCall(BlockJUnit4ClassRunner.java:309)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

java.lang.RuntimeException: Method format in android.text.format.DateFormat not mocked. See http://g.co/androidstudio/not-mocked for details.

    at android.text.format.DateFormat.format(DateFormat.java)
    at com.litongjava.criminalintent.DateFormatTest.formatTest(DateFormatTest.kt:15)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)


Process finished with exit code -1

一共出现了两个错误

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
java.lang.RuntimeException: Method getExternalStorageState in android.os.Environment not mocked. See http://g.co/androidstudio/not-mocked for details.

为什么会出现这个两个错误,原因是什么?
还有其他方法测试DateFormat.format(formatPattern,date)吗?

回复
阅读 101
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏