1

I'm trying to test the following method of 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();
    }

The populateViews() method looks like the following:

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);
    }

Now, in the test method looks like this:

@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();
    }

And here's the exception:

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)

The Views aren't inflated and the test fails. Is there any workaround for this issue?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Egor
  • 39,695
  • 10
  • 113
  • 130

1 Answers1

2

The reason is the ShadowDialogFragment is not implemented properly in Robolectric. There is show() method implemented and no onCreateDialog(). One solution could be implementing it yourself and push to the repository.

edit:

I've read your comment, and I've taken a closer look at sources. The dialog is created and attached in show() method of shadow implementation.

Try to invoke show() before testing the views:

dialogFragment.show(fragmentManager, "tag");
assertNotNull("dialog not null", dialogFragment.getDialog());

That will help you with FragmentManager.

Community
  • 1
  • 1
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • Actually onCreateDialog() runs well for other DialogFragment implementations of mine. Problem is in View inflating, and I need to test that the Views are inflated correctly and that proper data is bound. – Egor Nov 05 '12 at 10:47
  • Seems still the case in 2015. – Sebastian Roth Aug 16 '15 at 06:06