如何在Android中进行测试?

时间:2016-06-13 09:31:38

标签: android junit

我在Android中搜索测试,但我找不到我想要的,所以我需要问你这个问题。我想创建一个模拟发送表单的clase。我有这个,但我有一个错误:

package net.elinformaticoenganchado.sergio.crossfighters;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import net.elinformaticoenganchado.sergio.crossfighters.Configuration.ConfigurationEdit;

import org.junit.Test;

/**
 * @author Sergio Herrero Cruz <sergioherre96@gmail.com>
 * @package net.elinformaticoenganchado.sergio.crossfighters
 */
public class ConfigurationTest {

    @Test
    public void add_values(){
        ConfigurationEdit configurationEdit = new ConfigurationEdit();

        View v = configurationEdit.getView();

        EditText name = (EditText) v.findViewById(R.id.configuration_edit_name_edit);
        EditText phrase = (EditText) v.findViewById(R.id.configuration_edit_phrase_edit);

        name.setText("NombreTest");
        phrase.setText("PhraseTest Phrase Test");

        Button save = (Button) v.findViewById(R.id.configuration_edit_button_save);
        save.performClick();

    }
}

这是我的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    >

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/configuration_edit_name"
            android:textSize="@dimen/general_text_size"
            android:textStyle="bold"
            android:id="@+id/configuration_edit_name_text"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/configuration_edit_name_text"
            android:hint="@string/configuration_edit_name_hint"
            android:id="@+id/configuration_edit_name_edit"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/configuration_edit_name_edit"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:text="@string/configuration_edit_phrase"
            android:textSize="@dimen/general_text_size"
            android:textStyle="bold"
            android:id="@+id/configuration_edit_phrase_text"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/configuration_edit_phrase_text"
            android:hint="@string/configuration_edit_phrase_hint"
            android:id="@+id/configuration_edit_phrase_edit"
            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/configuration_edit_phrase_edit"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:text="@string/configuration_edit_button"
            android:textSize="@dimen/general_text_size"
            android:textStyle="bold"
            android:background="@color/colorSave"
            android:id="@+id/configuration_edit_button_save"
            />

    </RelativeLayout>

</ScrollView>

错误是:

  

java.lang.NullPointerException at   net.elinformaticoenganchado.sergio.crossfighters.ConfigurationTest.add_values(ConfigurationTest.java:25)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)     在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:363)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:137)at at   com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)     在   com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)     在   com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

感谢您的帮助!

0 个答案:

没有答案