当我尝试对 JDialog 对象中的某些方法进行单元测试时,我收到了 NullPointerException。我必须初始化对话框父级的模拟版本以及将使用的另一个类(除了调用静态方法。代码如下:
@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 );
}
...
}
用于构建对话框的代码在这里:
StructureCompareDialog( IControlFrame controlFrame )
{
super( (Frame) controlFrame, "title", true );
...
}
当调用超级构造函数时,我最终会在 java.awt.Window.addOwnerWindow(Window.java:2525) 处得到 NullPointerError"
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);
}
}
}
}
我知道我正在将静态和摇摆 gui 混合在一个有毒的漩涡中,但我别无选择。我被指示将一些单元测试与现有代码组合在一起。我不知道出了什么问题。
谢谢