测试Android应用程序:套件构建期间的异常

时间:2014-09-05 14:51:51

标签: android eclipse junit robotium

我有一个简单的应用程序,我正在尝试测试。它有两个活动和两个按钮(每个按钮转到另一个活动 - MainActivity和MainActivity2),我正在尝试对它进行一些简单的Robotium测试。我收到以下错误:

  

java.lang.RuntimeException:套件构造期间的异常   android.test.suitebuilder.TestSuiteBuilder $ FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)   在java.lang.reflect.Method.invokeNative(Native Method)at   android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)at   android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)at   android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)   在   android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1840)   引起:java.lang.reflect.InvocationTargetException at   java.lang.reflect.Constructor.constructNative(Native Method)at   java.lang.reflect.Constructor.newInstance(Constructor.java:423)at   android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)   在android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)   在   android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262)   在   android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)   在   android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)   在   android.app.ActivityThread.handleBindApplication(ActivityThread.java:5142)   在android.app.ActivityThread.access $ 1500(ActivityThread.java:156)at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1418)   在android.os.Handler.dispatchMessage(Handler.java:102)at   android.os.Looper.loop(Looper.java:157)at   android.app.ActivityThread.main(ActivityThread.java:5872)at   java.lang.reflect.Method.invokeNative(Native Method)at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:858)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)at   dalvik.system.NativeStart.main(Native方法)引起:   java.lang.NullPointerException at   android.view.ViewConfiguration.get(ViewConfiguration.java:338)at   android.view.View。(View.java:3490)at   android.view.View。(View.java:3547)at   android.widget.TextView。(TextView.java:674)at   android.widget.Button。(Button.java:107)at   android.widget.CompoundButton。(CompoundButton.java:68)at   android.widget.CheckBox。(CheckBox.java:68)at   android.widget.CheckBox。(CheckBox.java:64)at   android.widget.CheckBox。(CheckBox.java:60)at   com.example.twoactivities.test.RobotiumTest1。(RobotiumTest1.java:19)   ......还有18个

我已经尝试了所有我能找到的东西,但这些解决方案都不适合我。 (构造函数中没有参数,检查构建路径以确保JAR位于正确的位置,确保我的测试应用程序是“可调试的”,等等。)

我的代码在这里:

package com.example.twoactivities.test;

import com.example.twoactivities.*;
import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.CheckBox;
import android.widget.CheckBox;
import android.app.Activity;    

public class RobotiumTest1 extends ActivityInstrumentationTestCase2<MainActivity> {

        private Solo solo;

        public RobotiumTest1() {
            super(MainActivity.class);
        }

        @Override
        public void setUp() throws Exception {
            //setUp() is run before a test case is started. 
            //This is where the solo object is created.
            solo = new Solo(getInstrumentation(), getActivity());
        }

//      @Override
//      public void tearDown() throws Exception {
//          //tearDown() is run after a test case has finished. 
//          //finishOpenedActivities() will finish all the activities that have been opened during the test execution.
//          solo.finishOpenedActivities();
//      }


        public void testClickButton() throws Exception{

            solo.assertCurrentActivity("Expected to be on MainActivity activity", "MainActivity.class");
            solo.clickOnButton("Go to page 2");
            solo.assertCurrentActivity("Not on Activity 2", MainActivity2.class);
            solo.clickOnButton("Go to page 1");
            solo.assertCurrentActivity("Not on Activity 1", MainActivity.class);

        }       

    }

我有什么想法可以让这个测试运行?

谢谢, 斯蒂芬妮

编辑:

MainActivity

package com.example.twoactivities;

import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {
        final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
        final TextView text2 = (TextView) findViewById(R.id.checkBoxTextView);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void activity2(View view){
        Intent intent = new Intent(this,com.example.twoactivities.MainActivity2.class);
        startActivity(intent);
    }

    public void onCheckBoxClicked(View view){
        if (checkBox.isChecked()) {
            text2.setText("Checked");
        }
        else {
            ((TextView) findViewById(R.id.checkBoxTextView)).setText("Unchecked");
        }
    }
}

MainActivity2

package com.example.twoactivities;

import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

    public class MainActivity2 extends Activity {

           @Override
           protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main2);
           }

           public void activity1(View view){
              Intent intent = new Intent(this,com.example.twoactivities.MainActivity.class);
              startActivity(intent);
           }

}

我实际上没有改变任何东西(我不认为),因为它上次成功运行。

9月12日编辑: 在我尝试使用它之后,“solo”对象正在初始化。

package com.example.twoactivities.test;

import com.example.twoactivities.*;
import com.example.twoactivities.R;
import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.CheckBox;
import android.widget.TextView;


public class RobotiumTest1 extends ActivityInstrumentationTestCase2<MainActivity> {

        public Solo solo;
        private TextView checkBoxText; 
        public CheckBox checkBox1 ;

        public RobotiumTest1() {
            super(MainActivity.class);
        }

        @Override
        public void setUp() throws Exception {
            //setUp() is run before a test case is started. 
            //This is where the solo object is created.
            solo = new Solo(getInstrumentation(), getActivity());
            checkBoxText = (TextView) getActivity().findViewById(R.id.checkBoxTextView);
            checkBox1 = (CheckBox) getActivity().findViewById(R.id.checkBox1);
        }

        public void testClickButton() throws Exception{

            solo.assertCurrentActivity("Expected to be on MainActivity activity", "MainActivity");
            solo.clickOnButton("Go to page 2");
            solo.assertCurrentActivity("Not on Activity 2", MainActivity2.class);
            solo.clickOnButton("Go to page 1");
            solo.assertCurrentActivity("Not on Activity 1", MainActivity.class);

        }

1 个答案:

答案 0 :(得分:4)

线索在您发布的日志中。

Caused by: java.lang.NullPointerException
at android.view.ViewConfiguration.get(ViewConfiguration.java:338) 
at android.view.View.(View.java:3490) 
at android.view.View.(View.java:3547) 
at android.widget.TextView.(TextView.java:674) 
at android.widget.Button.(Button.java:107) 
at android.widget.CompoundButton.(CompoundButton.java:68) 
at android.widget.CheckBox.(CheckBox.java:68) 
at android.widget.CheckBox.(CheckBox.java:64) 
at android.widget.CheckBox.(CheckBox.java:60) at com.example.twoactivities.test.RobotiumTest1.(RobotiumTest1.java:19) ... 18 more

你有一个NullPointerException导致问题,这个NullPointerException在您的应用程序代码中,而不是在此处发布的代码(请注意,如果您添加它,我会在这里为您调查)< / p>

修改的 下面的代码是问题:

public class MainActivity extends Activity {
            final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
            final TextView text2 = (TextView) findViewById(R.id.checkBoxTextView);

问题是你试图在调用onCreate()之前找到实际上不存在的视图,因此这些视图将为null。将这些初始化移到onCreate(),你应该没问题。

接下来编辑以在评论中回答您的问题。

好的,现在我们离开Robotium并进入核心Java,你现在遇到的问题是范围,事情是你声明一个变量,它是可访问的,与你实例化的地方不同。您可以在与当前相同的范围内声明存在的变量,但在onCreate()中对其进行实例化。

package com.example.twoactivities;

import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends Activity {
        final CheckBox checkBox;
        final TextView text2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkBox = (CheckBox) findViewById(R.id.checkBox1);
        text2 = (TextView) findViewById(R.id.checkBoxTextView);
    }

    public void activity2(View view){
        Intent intent = new Intent(this,com.example.twoactivities.MainActivity2.class);
        startActivity(intent);
    }

    public void onCheckBoxClicked(View view){
        if (checkBox.isChecked()) {
            text2.setText("Checked");
        }
        else {
            ((TextView) findViewById(R.id.checkBoxTextView)).setText("Unchecked");
        }
    }
}