我正在为一个简单的Android应用程序编写单元测试。我已经能够让我的其他测试自己运行了。当我尝试将测试串在一起时,问题就出现了。我得到随机空指针异常错误,我不明白为什么。一个可能在按钮上,另一个时间在EditText上,有时则是一个微调器。我的代码列在下面。
/*
* This is a class to test various properties of temperature conversions. There are
* 3 widely used temperature scales, Fahrenheit, Celsius, and Kelvin. This class
* will test the various properties of the conversions from one temperature to another.
*
* The properties this class will test are as follows:
* 1. No zero temperature of any one temperature will convert to a zero temperature
* in another scale.
* 2. No temperature in the Celsius scale will convert to an equal temperature in
* the Kelvin scale.
* 3. For all temperatures above 574.6 degrees Fahrenheit, all temperatures in the
* Fahrenheit scale will be greater than the converted values in the Kelvin and
* Celsius scales.
* 4. For all Fahrenheit temperatures below -40 degrees Fahrenheit, the Fahrenheit
* temperatures will be less than their converted values in the Kelvin and
* Celsius scales.
* 5. For all temperatures in the range of {-40, ..., 574.6} degrees Fahrenheit, all
* temperatures in Fahrenheit will be less than or equal to the converted
* values in the Kelvin scale and less than or equal to the converted value in
* the Celsius scale.
*
*/
package com.example.unitconversion1.test;
import java.math.BigDecimal;
import java.util.ArrayList;
import com.example.unitconversion1.UnitConversion;
import com.example.unitconversion1.R;
import com.example.unitconversion1.helper.ListGenerator;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class PropertyBasedTest extends ActivityInstrumentationTestCase2<UnitConversion> {
private EditText from, to;
private Spinner spinnerFrom, spinnerTo;
private Button convertButton;
private UnitConversion unitConversion;
public static final int CELSIUS = 0, FARENHEIT = 1, KELVIN = 2, NUMTESTS = 10;
public PropertyBasedTest() {
super("com.example.unitconversion1.test", UnitConversion.class);
}
public PropertyBasedTest(Class<UnitConversion> activityClass) {
super(activityClass);
// TODO Auto-generated constructor stub
}
protected void setUp() throws Exception {
super.setUp();
unitConversion = getActivity();
from = (EditText) unitConversion.findViewById(R.id.value01);
to = (EditText) unitConversion.findViewById(R.id.value02);
spinnerFrom = (Spinner) unitConversion.findViewById(R.id.unit01);
spinnerTo = (Spinner) unitConversion.findViewById(R.id.unit02);
convertButton = (Button)unitConversion.findViewById(R.id.button1);
}
/*
* This test checks to make sure that no zero temperature in any given scale
* will convert to a zero value in another scale.
*/
public void testNonZeroConversion() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
from.setText("0");
spinnerFrom.requestFocus();
spinnerFrom.setSelection(KELVIN);
convertButton.performClick();
assertTrue(Double.parseDouble(to.getText().toString()) != 0);
spinnerFrom.requestFocus();
spinnerFrom.setSelection(CELSIUS);
convertButton.performClick();
assertTrue(Double.parseDouble(to.getText().toString()) != 0);
spinnerFrom.requestFocus();
spinnerFrom.setSelection(FARENHEIT);
convertButton.performClick();
assertTrue(Double.parseDouble(to.getText().toString()) != 0);
}
});
}
/*
* This test will check that no temperature in the Kelvin scale will convert
* to an equal value in the Celsius scale. The helper class ListGenerator
* will be used to generate random values in Kelvin. Then the test will convert
* the values to Celsius and compare the two.
*/
public void testNotEqualTemperatures() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
ArrayList<BigDecimal> testNotEqualTemps = ListGenerator.getReals(NUMTESTS);
spinnerFrom.requestFocus();
spinnerFrom.setSelection(KELVIN);
spinnerTo.requestFocus();
spinnerTo.setSelection(CELSIUS);
for(BigDecimal rand: testNotEqualTemps){
from.setText(String.valueOf(rand.doubleValue()));
convertButton.performClick();
assertTrue(!from.getText().toString().equals(to.getText().toString()));
}
}
});
}
/*
* This test checks that any temp in Fahrenheit above 574.6 will always yield a result
* in Kelvin and Celsius values that are less than the given Fahrenheit value.
*/
public void testFahrenheitGreater() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
ArrayList<BigDecimal> testGreaterTemps = ListGenerator.getRealsGreaterThan(NUMTESTS,574.6);
BigDecimal fromVal;
BigDecimal toVal;
spinnerFrom.requestFocus();
spinnerFrom.setSelection(FARENHEIT);
assertNotNull("Check to see button is not null",convertButton);
for(BigDecimal rand: testGreaterTemps){
assertNotNull("Check to see rand value is not null.", rand);
from.setText(String.valueOf(rand.doubleValue()));
spinnerTo.setSelection(CELSIUS);
convertButton.performClick();
fromVal = new BigDecimal(from.getText().toString());
toVal = new BigDecimal(to.getText().toString());
assertTrue("Fahrenheit is always bigger", fromVal.compareTo(toVal) > 0);
spinnerTo.setSelection(KELVIN);
convertButton.performClick();
toVal = new BigDecimal(to.getText().toString());
assertTrue("Fahrenheit is always bigger", fromVal.compareTo(toVal) > 0);
}
}
});
}
/*
* This test checks that any temp in Fahrenheit below -40 will always yield a result
* in Kelvin and Celsius values that are greater than the given Fahrenheit value.
*/
public void testFahrenheitLessThan() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
ArrayList<BigDecimal> testLesserTemps = ListGenerator.getRealsGreaterThan(NUMTESTS,-40.0d);
BigDecimal fromVal;
BigDecimal toVal;
spinnerFrom.requestFocus();
spinnerFrom.setSelection(FARENHEIT);
for(BigDecimal rand: testLesserTemps){
from.setText(String.valueOf(rand.doubleValue()));
spinnerTo.setSelection(CELSIUS);
convertButton.performClick();
fromVal = new BigDecimal(from.getText().toString());
toVal = new BigDecimal(to.getText().toString());
assertTrue("Fahrenheit is always bigger", fromVal.compareTo(toVal) < 0);
spinnerTo.setSelection(KELVIN);
convertButton.performClick();
toVal = new BigDecimal(to.getText().toString());
assertTrue("Fahrenheit is always bigger", fromVal.compareTo(toVal) < 0);
}
}
});
}
/*
* This test will check values of Fahrenheit between -40 and 574.6. For all temps
* in this range, the value should be larger than it's converted value in Celsius,
* and it should be less than th converted value in Kelvin.
*/
public void testFahrenheitInBetween() throws InterruptedException {
getActivity().runOnUiThread(new Runnable() {
public void run() {
ArrayList<BigDecimal> testInBetweenTemps = null;
try {
testInBetweenTemps = ListGenerator.getRealsInBetween(NUMTESTS, 574.6d, -40d);
Log.i(getName(), "ArrayList has been created for In between");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(getName(), "Something went wrong creating the ArrayList for Inbetween");
}
BigDecimal fromVal;
BigDecimal toVal;
spinnerFrom.requestFocus();
**spinnerFrom.setSelection(FARENHEIT)**;
for(BigDecimal rand: testInBetweenTemps){
from.setText(String.valueOf(rand.doubleValue()));
spinnerTo.setSelection(CELSIUS);
convertButton.performClick();
fromVal = new BigDecimal(from.getText().toString());
toVal = new BigDecimal(to.getText().toString());
assertTrue("Fahrenheit is bigger than Celsius", fromVal.compareTo(toVal) > 0);
spinnerTo.setSelection(KELVIN);
convertButton.performClick();
toVal = new BigDecimal(to.getText().toString());
assertTrue("Fahrenheit is less than Kelvin", fromVal.compareTo(toVal) < 0);
}
}
});
}
}
以下是我正在测试的活动的代码。
package com.example.unitconversion1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.AdapterView.OnItemSelectedListener;
public class UnitConversion extends Activity implements OnItemSelectedListener
{
double v1, v2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.tempUnits, android.R.layout.simple_spinner_item);
Spinner spinner01 = (Spinner) findViewById(R.id.unit01);
Spinner spinner02 = (Spinner) findViewById(R.id.unit02);
spinner01.setAdapter(adapter);
spinner01.setOnItemSelectedListener(this);
spinner02.setAdapter(adapter);
spinner02.setOnItemSelectedListener(this);
}
public void convertTemperature(View view) {
/*
* First we setup the views and then check to make sure we
* don't have a null value.
*/
int from = ((Spinner)findViewById(R.id.unit01)).getSelectedItemPosition(),
to = ((Spinner)findViewById(R.id.unit02)).getSelectedItemPosition();
EditText fromValue = (EditText)findViewById(R.id.value01);
if(fromValue.getText().length()==0) return;
/*
* Now we populate the class level variables with the values to convert.
*/
v1 = Double.parseDouble(fromValue.getText().toString());
v2 = (TempConverter.convert(v1, from, to));
/*
* Now we display the converted value in the second EditText view.00
*/
((EditText)findViewById(R.id.value02)).setText(Double.toString(v2));
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
}
最后,这是来自logcat的过滤消息。
12-01 05:21:50.409: E/AndroidRuntime(2018): FATAL EXCEPTION: main
12-01 05:21:50.409: E/AndroidRuntime(2018): java.lang.NullPointerException
12-01 05:21:50.409: E/AndroidRuntime(2018): at com.example.unitconversion1.test.PropertyBasedTest$2.run(PropertyBasedTest.java:203)
12-01 05:21:50.409: E/AndroidRuntime(2018): at android.os.Handler.handleCallback(Handler.java:725)
12-01 05:21:50.409: E/AndroidRuntime(2018): at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 05:21:50.409: E/AndroidRuntime(2018): at android.os.Looper.loop(Looper.java:137)
12-01 05:21:50.409: E/AndroidRuntime(2018): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-01 05:21:50.409: E/AndroidRuntime(2018): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 05:21:50.409: E/AndroidRuntime(2018): at java.lang.reflect.Method.invoke(Method.java:511)
12-01 05:21:50.409: E/AndroidRuntime(2018): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 05:21:50.409: E/AndroidRuntime(2018): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 05:21:50.409: E/AndroidRuntime(2018): at dalvik.system.NativeStart.main(Native Method)
我已尝试以不同的顺序进行测试,只看到之前的测试失败,然后看到空指针异常弹出另一个测试。有谁知道发生了什么事?我应该尝试让一个线程睡觉吗?或者我应该尝试组合测试,看看它们是否可以在同一个线程中工作?这种方式违背了测试独立的目的,但此时我只想取得一些进展。提前感谢您提供的任何帮助。
更新
我突出显示了代码块引用中的行,但似乎没有呈现为粗体。有问题的行如下:
spinnerFrom.setSelection(FARENHEIT);
这一行尤其在测试用例“testFahrenheitInBetween”中。
答案 0 :(得分:0)
我建议在创建过程的不同阶段使用断点或打印到控制台,以查看创建内容的位置以及不创建内容的位置。如果一切看起来都很好,然后你仍然得到nullPointerException,我会怀疑你传递对象引用的方式有问题。也许某些东西没有以你想要的方式返回。
另一个想法:通过单元测试,我始终明白将结果与文字值进行比较是最好的。换句话说,为您的方法提供一个设定值,并将结果与您知道它应该是的设定值进行比较,而不是像&gt;这样模糊的东西。 0等。
祝你好运。