【Android(Mockito(2.0.2-beta) - 无法模拟/间谍最后的课程)】归志宁无五亩园,读书本意在元元。这篇文章主要讲述Android:Mockito(2.0.2-beta) - 无法模拟/间谍最后的课程相关的知识,希望能为你提供帮助。
我在android Instrumented测试中使用qazxsw poi,qazxsw poi来模拟mockito-all
,这是一个最后的类,
我正进入(状态 -
version: '2.0.2-beta'
我的应用程序的
PowerManager
如下 -org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class android.os.PowerManager
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
at com.crsardar.handson.android.mockito.ExampleInstrumentedTest.useAppContext(ExampleInstrumentedTest.java:38)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
我的仪器测试案例如下 -
build.gradle
StackOverflow中现有的帖子都没有帮助,我们非常感谢任何帮助
答案可以通过以下方式使用ShadowPowerManager:
apply plugin: 'com.android.application'android {
compileSdkVersion 26
defaultConfig {
applicationId "com.crsardar.handson.android.mockito"
minSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:26.1.0'
}
}
}dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'
}
另一答案在研究了更多我发现 - 从这个版本的Mockito,它是不可能的。
另一答案好吧,我试着和当地的jvm一起跑,
package com.crsardar.handson.android.mockito;
import android.content.Context;
import android.os.PowerManager;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <
a href="http://d.android.com/tools/testing">
Testing documentation<
/a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {private Context mockedContext = null;
@Test
public void useAppContext() {PowerManager mockedPowerManager = mock(PowerManager.class);
System.out.println("Before mockedPowerManager = " + mockedPowerManager.hashCode());
mockedContext = mock(Context.class);
when(mockedContext.getSystemService(Context.POWER_SERVICE)).thenReturn(mockedPowerManager);
System.out.println("After mockedPowerManager = " + mockedContext.getSystemService(Context.POWER_SERVICE).hashCode());
}
}
这是输出,
private PowerManager powerManager;
private ShadowPowerManager shadowPowerManager;
@Before
public void before() {
powerManager =(PowerManager)ApplicationProvider.getApplicationContext().getSystemService(Context.POWER_SERVICE);
shadowPowerManager = shadowOf(powerManager);
shadowPowerManager.setIsScreenOn(false);
}
推荐阅读
- Android架构组件GithubBrowserSample单元测试理解
- Android Mockito如何模拟资源
- 集成CLion和Android NDK
- Android - 劫持点击[关闭]
- Android ListView performitemclick
- 如何在QML ApplicationWindow中获取activeFocusControl的className
- QML中的SVG未在Android上显示
- 如何阅读web.config文件中的appSettings部分()
- 在Azure App Service上启用gzip压缩