android模拟上下文,android – NullPointerException使用模拟上下文...

当我尝试在测试中使用模拟上下文创建AppCompatImageView时,我收到NullPointerException.对普通ImageView执行相同操作.

此测试通过:

import android.content.Context;

import android.widget.ImageView;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.mockito.Mock;

import org.mockito.MockitoAnnotations;

import org.mockito.junit.MockitoJUnitRunner;

import static junit.framework.Assert.assertNotNull;

@RunWith(MockitoJUnitRunner.class)

public class ParallaxViewTest {

@Mock

Context mContext;

@Before

public void setup() {

MockitoAnnotations.initMocks(this);

}

@Test

public void initWithContext() throws Exception {

assertNotNull(mContext);

ImageView imageView = new ImageView(mContext);

// AppCompatImageView imageView = new AppCompatImageView(mContext);

}

}

此测试未通过:

import android.content.Context;

import android.support.v7.widget.AppCompatImageView;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.mockito.Mock;

import org.mockito.MockitoAnnotations;

import org.mockito.junit.MockitoJUnitRunner;

import static junit.framework.Assert.assertNotNull;

@RunWith(MockitoJUnitRunner.class)

public class ParallaxViewTest {

@Mock

Context mContext;

@Before

public void setup() {

MockitoAnnotations.initMocks(this);

}

@Test

public void initWithContext() throws Exception {

assertNotNull(mContext);

// ImageView imageView = new ImageView(mContext);

AppCompatImageView imageView = new AppCompatImageView(mContext);

}

}

这是崩溃报告:

java.lang.NullPointerException

at android.support.v7.widget.ResourcesWrapper.(ResourcesWrapper.java:46)

at android.support.v7.widget.TintResources.(TintResources.java:34)

at android.support.v7.widget.TintContextWrapper.(TintContextWrapper.java:100)

at android.support.v7.widget.TintContextWrapper.wrap(TintContextWrapper.java:68)

at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:60)

at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:56)

at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:52)

at example.views.ParallaxViewTest.initWithContext(ParallaxViewTest.java:30)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:497)

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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

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 org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:68)

at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:74)

at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)

at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:497)

at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

这里的图书馆:

testCompile 'junit:junit:4.12'

testCompile 'org.mockito:mockito-core:2.7.18'

我怎么解决这个问题?

编辑

如何通过资源获得模拟上下文?

此测试未通过:

@Test

public void initWithContext() throws Exception {

assertNotNull(mContext); // PASS

assertNotNull(mContext.getResources()); // DO NOT PASS

// ImageView imageView = new ImageView(mContext);

// AppCompatImageView imageView = new AppCompatImageView(mContext);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值