【问题标题】:NullPointerException creating an AppCompatImageView with mock ContextNullPointerException 创建带有模拟上下文的 AppCompatImageView
【发布时间】:2017-03-23 10:11:22
【问题描述】:

当我尝试在测试中创建带有模拟 ContextAppCompatImageView 时,我收到了 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.<init>(ResourcesWrapper.java:46)
    at android.support.v7.widget.TintResources.<init>(TintResources.java:34)
    at android.support.v7.widget.TintContextWrapper.<init>(TintContextWrapper.java:100)
    at android.support.v7.widget.TintContextWrapper.wrap(TintContextWrapper.java:68)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
    at android.support.v7.widget.AppCompatImageView.<init>(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'

我该如何解决这个问题?


编辑

我怎样才能获得带有资源的模拟 Context

此测试未通过:

    @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);
    }

【问题讨论】:

  • 是的,我找到了 when().thenReturn() 功能。这正是我正在寻找的。谢谢
  • 您可能想检查该 DEEP 存根是否会自动执行此操作;但也许我也只是把那个功能弄错了。很高兴你得到了你想要的东西!
  • 最后一个提示:你做得过火了。您已经在使用 Mockito 跑步者,因此根本不需要调用“MockitoAnnotations.initMocks(this);”。当您出于某种原因使用 Mockito 运行器时,您只需要手动 initMocks()。因此,实际上根本不需要您的“设置”方法(到目前为止;这可能会改变,例如,当您想将一些通用的 when/then 子句放入 setup 方法时;因为 all需要它们> 你的测试)

标签: android unit-testing exception mockito android-testing


【解决方案1】:

当您查看堆栈跟踪 (ResourcesWrapper) 中列出的类的源代码时,您会发现:

public ResourcesWrapper(Resources resources) {
  super(resources.getAssets(), resources.getDisplayMetrics(), 

第 46 行是带有 super() 的那一行。

进一步查看堆栈跟踪中的类,您可能会遇到:

 private TintContextWrapper(@NonNull final Context base) {
  super(base);
  ...
  mResources = new VectorEnabledTintResources(this, base.getResources());

所以,长话短说,是的,您在代码中为 new AppCompatImageView() 提供了一个 non null 模拟对象。但是,您正在调用的代码 正在调用该 mocked 对象上的 方法。当然,这就是您首先创建模拟的原因。但猜猜怎么了;默认情况下,模拟框架将为任何方法调用返回 null

换句话说:您必须了解该模拟上会发生哪些调用;这样您就可以准备模拟以返回 non null 的东西了!

确切地说:我并不是说 TintContextWrapper() 中的那一行导致了这个 NPE;我主要是说:当您将一个模拟对象放入其他代码时,您必须准备该模拟以在将发生的那些方法调用上返回合理的结果。这很可能意味着您必须创建更多模拟;这样mockedContext.getResources() 之类的东西就会返回 non-null 结果。

换句话说:你必须

  • 识别在该模拟对象上发生的调用
  • 那么您必须确保这些调用将返回非空值(例如通过再次返回模拟对象)。

除此之外:更可能的是,真正的答案是使用 Android 特定 模拟框架。准备你的模拟以让他们“做正确的事”很容易变成大量工作。

也许简单的答案是使用 mockito 中的deep stubbing,只需编写

@Mock (answer = Answers.RETURNS_DEEP_STUBS)

但是您需要阅读/尝试一下;我自己没用过。

鉴于您的最新情况:您需要配置您的模拟,例如

when(context.getResources()).thenReturn(someOtherMock);

例如!这就是模拟的全部点:您可以控制调用方法时发生的事情!

【讨论】:

  • 您正在解释如何搜索解决方案,我很感激,谢谢,但我认为答案应该更容易,因为我正在使用 Android 文档库进行测试并创建一个简单的 AppCompatImageView应该不难。 ImageView 完美运行,为什么 AppCompatImageView 不行?
  • 我假设我不是唯一一个遇到这个问题的人,但是在您最后的评论之后,答案是如何简单地将资源设置为上下文。
猜你喜欢
  • 2013-01-04
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
相关资源
最近更新 更多