I have to write a JUnit test method for a short program that lets the user add/remove/browse images in a collection. (Adding an image is done with a JFileChooser) The test method that I wrote exhibits some really bizarre behavior; it passes every so often, but fails most of the time and produces the following error message:
java.lang.NullPointerException
at javax.swing.plaf.basic.BasicListUI.updateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(Unknown Source)
at javax.swing.plaf.basic.BasicListUI.getCellBounds(Unknown Source)
at javax.swing.JList.getCellBounds(Unknown Source)
at javax.swing.JList.ensureIndexIsVisible(Unknown Source)
at sun.swing.FilePane.ensureIndexIsVisible(Unknown Source)
at sun.swing.FilePane.doDirectoryChanged(Unknown Source)
at sun.swing.FilePane.propertyChange(Unknown Source)
at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
at java.beans.PropertyChangeSupport.firePropertyChange(Unknown Source)
at java.awt.Component.firePropertyChange(Unknown Source)
at javax.swing.JFileChooser.setCurrentDirectory(Unknown Source)
at javax.swing.JFileChooser.setSelectedFile(Unknown Source)
at GalleryPanelTest.testGalleryPanel(GalleryPanelTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at student.TestCase.runBare(TestCase.java:108)
at student.GUITestCase.runBare(GUITestCase.java:233)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:230)
at junit.framework.TestSuite.run(TestSuite.java:225)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Since I can't reproduce the behavior by actually running and using the program, I think it's a problem with how I wrote the test method and not any part of the actual program, but I can't seem to figure out what it is. Here's the code for the test method:
public class GalleryPanelTest
extends GUITestCase
{
// ----------------------------------------------------------
/**
* Tests the GalleryPanel.
*/
public void testGalleryPanel()
{
GalleryPanel panel = new GalleryPanel();
showInFrame( panel );
JButton add = getComponent( JButton.class, "Add" );
JButton remove = getComponent( JButton.class, "Remove" );
JButton next = getComponent( JButton.class, "Next" );
JButton prev = getComponent( JButton.class, "Previous" );
click(add);
JFileChooser chooser = getComponent( JFileChooser.class );
File file1 = new File("C:/Users/Eric/Desktop/test images/1.png");
chooser.setSelectedFile(file1);
chooser.approveSelection();
click(add);
chooser = getComponent( JFileChooser.class );
File file2 = new File("C:/Users/Eric/Desktop/test images/2.png");
chooser.setSelectedFile(file2);
chooser.approveSelection();
click(next);
click(prev);
click(remove);
click(remove);
}
}
Any help would be greatly appreciated.