无法在未调用Looper

无法在未调用Looper

本文介绍了仪器化的单元类测试-无法在未调用Looper.prepare()的线程内创建处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我到处都看过教程,但找不到我想要的答案.我目前在这里关注Google的教程: https: //developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

Sorry I've looked at tutorials all over the place and not found the answer I'm looking for. I'm following Google's Tutorial at the moment here: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

我正在尝试创建一个有工具的测试,当我运行它时出现错误:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I'm trying to create an Instrumented test and when I run it I'm getting the error : java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

所以我的测试如下:

    package testapp.silencertestapp;

import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class MainActivityTest {

    private MainActivity testMain;

    @Before
    public void createActivity(){
        testMain = new MainActivity();
    }

    @Test
    public void checkFancyStuff(){
        String time = testMain.createFancyTime(735);

        assertThat(time, is("07:35"));
    }
}

我正在尝试在主要活动中运行一个方法,如下所示(这是摘录):

And I'm trying to run a method inside the main activity is as follows (this is an extract):

public class MainActivity extends AppCompatActivity {

    private TimePicker start;
    private TimePicker end;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        start = (TimePicker) findViewById(R.id.startPicker);
        end = (TimePicker) findViewById(R.id.endPicker);
}

 String createFancyTime(int combinedTime) {

        StringBuilder tempString = new StringBuilder(Integer.toString(combinedTime));
        if(tempString.length()==4){
            tempString = tempString.insert(2, ":");
        }
        else if (tempString.length()==3){
            tempString = tempString.insert(1, ":");
            tempString = tempString.insert(0, "0");
        }
        else if(tempString.length()==2){
            tempString = tempString.insert(0, "00:");
        }
        else if(tempString.length()==1){
            tempString = tempString.insert(0, "00:0");
        }
        return tempString.toString();
    }

我认为这是一个问题,因为我没有正确启动服务或进行某些操作-我尝试了几种方法,但是到处都是错误.在这里搜索该错误很普遍,但是与测试无关,所以想知道是否有人可以向我指出正确的方向,以便我可以测试此类中的方法?

I presume that this is an issue because I'm not starting the service up correctly or something - I've tried using several ways, but I just get errors everywhere. Searching on here and this error is popular, but not in relation to testing, so wondered if anyone could kindly point me in the right direction so I can test the methods in this class?

推荐答案

由于未正确为测试中的系统(MainActivity)设置Looper,因此发生了错误.

The error is happening because the Looper is not correctly set up for your system under test (MainActivity).

ActivityFragment等具有无参数构造函数时,它们被设计为由Android OS实例化,因此调用MainActivity = new Activity()不足以获取完全可操作的实例,其中包含HandlerLooper.

While Activity, Fragment etc. have no-args constructors, they are designed to be instantiated by the Android OS and as such calling MainActivity = new Activity() is not enough to obtain a fully-operational instance complete with Handler and Looper.

如果要继续进行测试,有两种选择:

There are two options if you want to proceed with testing:

  1. 如果要测试活动的真实实例,则必须为仪器单元测试(androidTest不是test),而@TestRule会导致Android操作系统正确实例化Activity实例:

  1. If you want to test a real instance of an Activity it will have to be an instrumented unit test (androidTest not test) and the @TestRule will cause the Android OS to correctly instantiate an instance of Activity:

@Rule
public ActivityTestRule<MainActivity> mActivityRule =
    new ActivityTestRule(MainActivity.class);

  • 如果您希望继续编写在IDE中运行的本地单元测试,则可以使用 Robolectric . Robolectric将正确地对影子活动的行为进行存根,以便您可以测试依赖Looper等的组件.请注意其中涉及一些设置.

  • If you would prefer to keep writing local unit tests that run in the IDE you can use Robolectric. Robolectric will correctly stub behaviour on a shadow Activity so that you can test components that rely on Looper etc. Note there is some setup involved.

    这篇关于仪器化的单元类测试-无法在未调用Looper.prepare()的线程内创建处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 07-30 22:33