单元测试Android Mockito中的Intent Extras [英] Intent extras in Unit test Android Mockito

查看:318
本文介绍了单元测试Android Mockito中的Intent Extras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图验证是否将特定的附加功能添加到Intent中,但是在Android单元测试中,我一直都没有看到Intent.我有以下课程需要测试:

I am trying to verify that specific extras are added to Intent, but all the time I get null for the Intent in Unit test Android. I have the following class that need to be test:

public class TestClass extends SomeNotifier {
        private Intent mIntent = new Intent("testintent");

        public TestClassConstructor(Context context) {
            super(context);
        }

        @Override
        public void notifyChange() {
            mIntent.putExtra("Key one", 22);
            mIntent.putExtra("Key two", 23);
            mIntent.putExtra("Key three", 24);
            getContext().sendBroadcast(mIntent);
        }
    }

下面是测试(我也尝试过用模拟模拟,但是结果是一样的,额外内容还是空的):

And the test is the following (I tried with mockIntent as well, but the result is the same, again the extras are null):

@RunWith(MockitoJUnitRunner.class)
public class TestClassTest {

  @Mock
  Context mMockContext;

  @Test
  public void sendBroadcastTest() {

  ArgumentCaptor<Intent> argument = ArgumentCaptor.forClass(Intent.class);

  TestClass testClassNotifier = new TestClass (mMockContext);
  testClassNotifier.notifyChange();
  verify(mMockContext).sendBroadcast(argument.capture());


 Intent intent = argument.getValue();
 //THE INTENT IS NULL
 Assert.assertTrue(intent.hasExtra("Key one"));

    }
}

您有什么建议,我应该如何进行此测试? 预先感谢

Do you have any suggestion how should I make this test to work? Thanks in advance

推荐答案

Intent和其他Android运行时类(例如Context)仅在物理Android手机和仿真器上可用.对于本地单元测试,将为您提供运行时的残存版本,其中Intent等.方法默认情况下将返回null.请参见此问题以获取解释.

Intent and other Android runtime classes like Context are, by default, only available on physical Android handsets and emulators. For the local unit tests you are provided with a stubbed out version of the runtime where Intent etc. methods will return null by default. See this question for an explanation.

这意味着您当前的测试正在针对Intent的存根版本进行验证,默认情况下,该版本将为所有方法调用返回null.

This means your current test is verifying against a stubbed out version of Intent which will return null for all of the method calls by default.

有一些解决方法-您可以将测试转换为仪器化的单元测试,这意味着您必须在真实的​​手机或仿真器上运行它.或者,您可以用自己控制的类包装Intent类.

There are some ways around this - you can convert the test to an instrumented unit test which would mean you would have to run it on a real handset or emulator. Alternatively, you can wrap the Intent class with a class you control.

也许最好的解决方案是使用 Robolectric 之类的框架.这将为您提供重要的Android类(例如Intent)的有效测试双打(称为阴影),用于在您的IDE中运行的本地单元测试.

Perhaps the best solution is to use a framework like Robolectric. This will provide you with working test doubles (called shadows) of important Android classes like Intent for local unit tests that run in your IDE.

一旦您仔细安装了Robolectric,您只需添加以下内容即可进行测试:

Once you have carefully installed Robolectric you would simply add the following to make your test work:

@RunWith(RobolectricTestrunner.class)
public class TestClassTest {

顺便说一句,请确保您使用的是Android上Mockito的正确依赖项:

As an aside, please make sure you are using the correct dependencies for Mockito on Android:

testCompile 'org.mockito:mockito-core:2.8.9'
androidTestCompile 'org.mockito:mockito-android:2.8.9'

这篇关于单元测试Android Mockito中的Intent Extras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆