我正在尝试测试 a 的以下方法DialogFragment
:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View rootView = View.inflate(getActivity(), R.layout.fragment_fileproperties_layout, null);
thumbImageView = (ImageView) rootView.findViewById(R.id.filePropertiesThumb);
nameTextView = (TextView) rootView.findViewById(R.id.filePropertiesName);
typeTextView = (TextView) rootView.findViewById(R.id.filePropertiesTypeValue);
locationTextView = (TextView) rootView.findViewById(R.id.filePropertiesLocationValue);
sizeTextView = (TextView) rootView.findViewById(R.id.filePropertiesSizeValue);
numberOfFilesLayout = (LinearLayout) rootView
.findViewById(R.id.filePropertiesNumOfFilesLayout);
numberOfFilesTextView = (TextView) rootView
.findViewById(R.id.filePropertiesNumOfFilesValue);
lastModifiedLayout = (LinearLayout) rootView
.findViewById(R.id.filePropertiesLastModifiedLayout);
lastModifiedTextView = (TextView) rootView
.findViewById(R.id.filePropertiesLastModifiedValue);
hiddenCheckbox = (CheckBox) rootView.findViewById(R.id.filePropertiesHiddenCheckbox);
hiddenCheckbox.setOnCheckedChangeListener(this);
typeCategoryTextView = (TextView) rootView.findViewById(R.id.filePropertiesCategoryType);
infoCategoryTextView = (TextView) rootView.findViewById(R.id.filePropertiesCategoryInfo);
attributesCategoryTextView = (TextView) rootView
.findViewById(R.id.filePropertiesCategoryAttributes);
populateViews();
if (selectedFiles.size() == 1) {
if (selectedFiles.get(0).isFile()) {
populateViewsFile();
} else {
populateViewsFolder();
}
} else {
populateViewsMultiple();
}
toggleHiddenOnExit = false;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(rootView).setTitle(R.string.properties)
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, this);
return builder.create();
}
该populateViews()
方法如下所示:
private void populateViews() {
final int backgroundColor = getResources().getColor(android.R.color.darker_gray);
final int textColor = getResources().getColor(android.R.color.black);
typeCategoryTextView.setText(R.string.type);
typeCategoryTextView.setBackgroundColor(backgroundColor);
typeCategoryTextView.setTextColor(textColor);
infoCategoryTextView.setText(R.string.info);
infoCategoryTextView.setBackgroundColor(backgroundColor);
infoCategoryTextView.setTextColor(textColor);
attributesCategoryTextView.setText(R.string.attributes);
attributesCategoryTextView.setBackgroundColor(backgroundColor);
attributesCategoryTextView.setTextColor(textColor);
}
现在,在测试方法中看起来是这样的:
@Test
public void testCreateDialogForAFile() {
file = new File(Environment.getExternalStorageDirectory(), "/file.txt");
try {
file.createNewFile();
} catch (IOException e) {
LogUtils.e(e.getMessage(), e);
}
List<File> files = new ArrayList<File>();
files.add(file);
dialog.setSelectedFiles(files);
assertNotNull("onCreateDialog not null", dialog.onCreateDialog(null));
file.delete();
}
这是一个例外:
java.lang.NullPointerException
at com.foo.bar.fragment.dialog.FooDialogFragment.onCreateDialog(FooDialogFragment.java:93)
at com.foo.bar.fragment.dialog.FooDialogFragmentTest.testCreateDialogForAFile(FooDialogFragmentTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:292)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Views
没有充气,测试失败。这个问题有什么解决方法吗?