如何创建支持重绘功能的 JFrame/JPanel 的 Mock
How do I create a Mock of a JFrame/JPanel that supports repaint functionality
我的 Swing 应用程序单元测试有点问题。
我想做的是将一个可绘制的对象传递给我的 JPanel,每次我这样做时它都应该重新绘制自己。
基本场景就这么多,现在开始我的单元测试:
public class GraphicViewImplTest {
private JFrame frame;
private GraphicViewImpl view; //This is my JPanel
private GraphicLineSpy firstGraphicLine;
private Line firstLine;
@Before
public void setUp() throws Exception {
frame = new JFrame();
view = new GraphicViewImpl();
frame.add(view);
frame.setVisible(true);
firstLine = new Line();
firstLine.setStart(new Point(11, 12));
firstLine.setEnd(new Point(21, 22));
firstGraphicLine = new GraphicLineSpy(firstLine);
}
@Test
public void whenReceivingLine_shouldPaintLine() {
view.receiveShape(firstGraphicLine);
assertTrue(firstGraphicLine.wasPainted());
}
}
如您所见,我正在将 GraphicLineSpy 传递到我的视图。 GraphicLine class 基本上是 Line class 的装饰器,知道如何在 Swing 中绘制线条。 GraphicLineSpy 覆盖了 GraphicLine 的 paint 方法,只是将一个标志设置为 true,因此我可以检查是否调用了 paint 方法。
现在开始我的 GraphicView JPanel 的实现:
public class GraphicViewImpl extends JPanel implements GraphicView, Observer {
protected GraphicViewPresenter presenter;
protected List<GraphicShape> graphicShapeList = new LinkedList<>();
@Override
public void receiveShape(GraphicShape graphicShape) {
graphicShapeList.add(graphicShape);
graphicShape.addObserver(this);
repaint();
}
@Override
public void removeShape(GraphicShape graphicShape) {
graphicShapeList.remove(graphicShape);
graphicShape.removeObserver(this);
repaint();
}
public void setPresenter(GraphicViewPresenter presenter) {
this.presenter = presenter;
}
@Override
public void update() {
repaint();
}
@Override
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
for (GraphicShape graphicShape : graphicShapeList)
graphicShape.paint(graphics);
}
}
现在,我的问题是,当我 运行 这些测试时,他们说我的 GraphicLine 没有绘制。但是,当我实际 运行 程序并添加一个新的 GraphicLine 时,它工作得非常好,我所有的形状都被绘制了。我在测试设置中遗漏了什么吗?
此外,这可能是最重要的部分,我想每次 运行 我的测试都启动整个 JFrame 并不是一个真正的最佳解决方案,所以我想知道我最好怎么做创建一个不会破坏整个重绘功能的测试替身。
提前感谢您的任何提示!
我认为您应该专注于测试 您的 代码而不是 JPanel 实现,因此您应该使用 Mockito 框架(或任何其他框架)模拟这些其他依赖项:
public class GraphicViewImplTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private Graphics2D graphics; // not tested dependency
@Mock
private GraphicShape firstLine; // not tested dependency
private GraphicViewImpl view; //This is my JPanel
@Before
public void setUp() throws Exception {
view = spy(new GraphicViewImpl());
doNothing().when(view).repaint();
}
@Test
public void whenReceivingLine_shouldPaintLine() {
view.receiveShape(firstGraphicLine);
verify(view).repaint();
verify(firstLine,never()).paint(graphics);
view.paintComponent(graphics);
verify(firstLine).paint(graphics);
}
}
我的 Swing 应用程序单元测试有点问题。 我想做的是将一个可绘制的对象传递给我的 JPanel,每次我这样做时它都应该重新绘制自己。
基本场景就这么多,现在开始我的单元测试:
public class GraphicViewImplTest {
private JFrame frame;
private GraphicViewImpl view; //This is my JPanel
private GraphicLineSpy firstGraphicLine;
private Line firstLine;
@Before
public void setUp() throws Exception {
frame = new JFrame();
view = new GraphicViewImpl();
frame.add(view);
frame.setVisible(true);
firstLine = new Line();
firstLine.setStart(new Point(11, 12));
firstLine.setEnd(new Point(21, 22));
firstGraphicLine = new GraphicLineSpy(firstLine);
}
@Test
public void whenReceivingLine_shouldPaintLine() {
view.receiveShape(firstGraphicLine);
assertTrue(firstGraphicLine.wasPainted());
}
}
如您所见,我正在将 GraphicLineSpy 传递到我的视图。 GraphicLine class 基本上是 Line class 的装饰器,知道如何在 Swing 中绘制线条。 GraphicLineSpy 覆盖了 GraphicLine 的 paint 方法,只是将一个标志设置为 true,因此我可以检查是否调用了 paint 方法。
现在开始我的 GraphicView JPanel 的实现:
public class GraphicViewImpl extends JPanel implements GraphicView, Observer {
protected GraphicViewPresenter presenter;
protected List<GraphicShape> graphicShapeList = new LinkedList<>();
@Override
public void receiveShape(GraphicShape graphicShape) {
graphicShapeList.add(graphicShape);
graphicShape.addObserver(this);
repaint();
}
@Override
public void removeShape(GraphicShape graphicShape) {
graphicShapeList.remove(graphicShape);
graphicShape.removeObserver(this);
repaint();
}
public void setPresenter(GraphicViewPresenter presenter) {
this.presenter = presenter;
}
@Override
public void update() {
repaint();
}
@Override
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
for (GraphicShape graphicShape : graphicShapeList)
graphicShape.paint(graphics);
}
}
现在,我的问题是,当我 运行 这些测试时,他们说我的 GraphicLine 没有绘制。但是,当我实际 运行 程序并添加一个新的 GraphicLine 时,它工作得非常好,我所有的形状都被绘制了。我在测试设置中遗漏了什么吗?
此外,这可能是最重要的部分,我想每次 运行 我的测试都启动整个 JFrame 并不是一个真正的最佳解决方案,所以我想知道我最好怎么做创建一个不会破坏整个重绘功能的测试替身。
提前感谢您的任何提示!
我认为您应该专注于测试 您的 代码而不是 JPanel 实现,因此您应该使用 Mockito 框架(或任何其他框架)模拟这些其他依赖项:
public class GraphicViewImplTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private Graphics2D graphics; // not tested dependency
@Mock
private GraphicShape firstLine; // not tested dependency
private GraphicViewImpl view; //This is my JPanel
@Before
public void setUp() throws Exception {
view = spy(new GraphicViewImpl());
doNothing().when(view).repaint();
}
@Test
public void whenReceivingLine_shouldPaintLine() {
view.receiveShape(firstGraphicLine);
verify(view).repaint();
verify(firstLine,never()).paint(graphics);
view.paintComponent(graphics);
verify(firstLine).paint(graphics);
}
}