简体   繁体   English

测试适配器时出现Robolectric NullPointerException

[英]Robolectric NullPointerException when testing adapters

I have a problem with Robolectric. 我在Robolectric有问题。 I'm trying to test an adapter I'm going to implement, and my first test should check if an adapter is instantiated and all the fields are created and ok. 我正在尝试测试将要实现的适配器,而我的第一个测试应该检查适配器是否已实例化,并且所有字段都已创建且正常。

In my adapter constructor I have this: 在我的适配器构造函数中,我有以下内容:

public MyAdapter(Context context) {
    this.mContext = context;
    this.mLayoutInflater =  LayoutInflater.from(mContext);
    this.listOfItems= new ArrayList<Items>();
}

and my Test looks like this 我的测试看起来像这样

@Test
public void newInstanceNoDataSetTest(){
    //Initialize a new adapter and test if all the needed fields are created
    MyAdapter adapterNoDataSet = new MyAdapter(MyApp.getInstance().getApplicationContext());

    Assert.assertNotNull(adapterNoDataSet);
    testFields(adapterNoDataSet);

    //Check to see if the count of the adapter is 0 (no items have been set)
    Assert.assertEquals(adapterNoDataSet.getCount(),0);
}

Every time I run my test it fails with NullpointerException at this line this.mLayoutInflater = LayoutInflater.from(mContext); 每次我运行测试时,在此行this.mLayoutInflater = LayoutInflater.from(mContext);失败,并NullpointerException this.mLayoutInflater = LayoutInflater.from(mContext); I think it has to do with the fact that that's a static call. 我认为这与静态调用有关。

Can anybody help me with that? 有人可以帮我吗?

Thanks, Ark 谢谢,方舟

I think the issue probably comes from your usage of MyApp.getInstance() , which I assume points to your Application. 我认为问题可能来自您对MyApp.getInstance() ,我认为这指向您的Application。 When running the app normally, the Application will be created, so your instance won't be null. 正常运行该应用程序时,将创建该应用程序,因此您的实例不会为空。 When running with Robolectric, on the other hand, the Application instance is created by Robolectric, and is accessible through RuntimeEnvironment.application , if you're using Robolectric 3.0 or Robolectric.application otherwise. 另一方面,在使用Robolectric运行时,Application实例是由Robolectric创建的,并且可以通过RuntimeEnvironment.application访问(如果您使用的是Robolectric 3.0或Robolectric.application Your code should look like this: 您的代码应如下所示:

@Test
public void newInstanceNoDataSetTest(){
    //Initialize a new adapter and test if all the needed fields are created
    MyAdapter adapterNoDataSet = new MyAdapter((MyApp)RuntimeEnvironment.application);

    Assert.assertNotNull(adapterNoDataSet);
    testFields(adapterNoDataSet);

    //Check to see if the count of the adapter is 0 (no items have been set)
    Assert.assertEquals(adapterNoDataSet.getCount(),0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 测试 Robolectric NullPointerException 创建 PendingIntent - Android testing Robolectric NullPointerException creating PendingIntent Robolectric:使用ViewHolder模式测试ListView导致NullPointerException - Robolectric: testing a ListView with ViewHolder pattern cause NullPointerException 在Robolectric中运行时片段提交的NullPointerException - NullPointerException on fragment commit when running in Robolectric 在Android Studio上测试Robolectric时出错 - Error when testing Robolectric on Android Studio 使用Robolectric 3和ORMLite测试时使用本地数据库 - Use local db when testing with Robolectric 3 and ORMLite 使用Robolectric进行测试时,Android WorkManager出错 - Android WorkManager has error when testing with Robolectric 使用Robolectric和ORMLite测试时使用模拟数据库 - Use mock db when testing with Robolectric and ORMLite 不能使用Robolectric的FakeHttpLayer(调用getFakeHttpLayer时为NullPointerException) - Cannot use FakeHttpLayer of Robolectric (NullPointerException when calling getFakeHttpLayer) 在Robolectric下从服务获取WindowManager时发生NullPointerException - NullPointerException when getting WindowManager from Service under Robolectric 实现适配器时出现错误java.lang.NullPointerException - I get the error java.lang.NullPointerException when implementing Adapters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM