在我的FEST测试中,我试图断言JButton有一个特定的ImageIcon。我在org.test.swing.fixture.JButtonFixture 上找不到相应的方法
您可以编写一个自己的ButtonFixture Wrapper,它提供了一个方法。
IconButtonFixture iconButtonFixture = new IconButtonFixture(buttonFixture.robot, buttonFixture.target);
iconButtonFixture.requireIcon(new ImageIcon( "file:/C:/Users/admin/workspace/Project/bin/image/icon.gif" ));
IconButtonFixture类:
import static org.fest.swing.edt.GuiActionRunner.execute;
public class IconButtonFixture extends JButtonFixture {
private IconButtonDriver driver;
public IconButtonFixture(Robot robot, JButton target) {
super(robot, target);
driver = new IconButtonDriver(robot);
}
public JButtonFixture requireIcon(Icon icon) {
driver.requireIcon(target, icon);
return this;
}
private class IconButtonDriver extends AbstractButtonDriver {
public IconButtonDriver( Robot robot ) {
super( robot );
}
public void requireIcon(final JButton button, Icon icon) {
Icon buttonIcon = execute(new GuiQuery<Icon>() {
protected Icon executeInEDT() {
return button.getIcon();
}
});
if(!icon.toString().equals( buttonIcon.toString() )) {
Assert.failNotEquals( "The Button has not the expected Icon.", icon, button.getIcon() );
}
}
}
}
使用target怎么样?
Assert.assertNotNull( jButtonFixture.target.getIcon() );