Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powermock, Mockito nullpointerexception when calling super() JDialog

I am getting a NullPointerException when I try to unit test some methods in a JDialog object. I have to initialize a mock version of the parent of the dialog as well as another class that will be used (in addition to calling a static method. The code is as follows:

@RunWith( PowerMockRunner.class )
@PrepareForTest( ControlFileUtilities.class )
public class StructCompDlgTest 
{
  @Before
  public void setUp() throws Exception
  {
    controlFrame    = org.mockito.Mockito.mock( ControlFrame.class );
    structCmpDlg    = new StructureCompareDialog( controlFrame );
    serverPipeline  = org.mockito.Mockito.mock( ServerPipeline.class );
  }
...
}

The code that is called for constructing the dialog is here:

StructureCompareDialog( IControlFrame controlFrame )
{
 super( (Frame) controlFrame, "title", true );
 ...
}

when the super constructor is called I will eventually get an NullPointerError at java.awt.Window.addOwnerWindow(Window.java:2525)"

void addOwnedWindow(WeakReference weakWindow) {
  if (weakWindow != null) {
    synchronized(ownedWindowList) {  ***<<------ offending line***
      // this if statement should really be an assert, but we don't
      // have asserts...
      if (!ownedWindowList.contains(weakWindow)) {
        ownedWindowList.addElement(weakWindow);
      }
    }
  }
}

I know I am mixing up statics and swing gui in a toxic swirl but I have no choice. I was given the instruction to throw together some unit tests with existing code. I have no idea what is going wrong.

Thanks

like image 657
sldahlin Avatar asked Jun 27 '12 19:06

sldahlin


1 Answers

Looks tricky! Essentially you're going to have to find all of the methods that are being called on controlFrame as part of the constructor, and then sprinkle some calls to

when(controlFrame.methodCalled()).thenReturn(somethingSensible);

If this is looking like a difficult chore, how about trying to create a default implementation of IControlFrame that you create as part of your test setUp() and using that instea of a mock.

I had a similar issue a while ago where I was trying to unit test a spring JMS listener. Rightly or wrongly, I got a working solution by creating my own default implementation of DefaultMessageListenerContainer which was giving me similar issues to what you are describing. My solution involved extending the real implementation with my own test specific version which looked like this

/**
 * Empty mocked class to allow unit testing with spring references to a
 * DefaultMessageListenerContainer. The functionality on this class should never be
 * called so just override and do nothing.  
 */
public class MockDefaultMessageListenerContainer extends DefaultMessageListenerContainer {

    public MockDefaultMessageListenerContainer() {
    }

    public void afterPropertiesSet() {
    }

    @Override
    protected Connection createConnection() throws JMSException {
        return null;
    }
}

In my example I was able to get my tests running by passing back a null value for the problemmatic createConnection() method. Perhaps the same approach can help you.

like image 160
Brad Avatar answered Oct 13 '22 00:10

Brad