Swingアプリケーションの単体テストに少し問題があります。 私がやってみたいのは、JPanelにペイント可能なオブジェクトを渡して、それを再描画する必要があるたびにです。再描画機能をサポートするJFrame/JPanelのモックを作成するには
今の私のユニットテストの基本的なシナリオでは、あまりにも多くの:あなたは私が私の見解にGraphicLineSpyを渡している見ることができるように
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());
}
}
。 GraphicLineクラスは基本的にLineクラスのデコレータで、Swingで線をペイントする方法を知っています。 GraphicLineSpyは、GraphicLineのpaintメソッドをオーバーライドし、単にフラグをtrueに設定するので、paintメソッドが呼び出されたかどうかを確認できます。
今の私のグラフィック・ビューのJPanelのimplemenationへ:
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を追加すると、すべてのShapeがペイントされます。テスト設定で何か不足していますか?
さらに、これはおそらく最も重要な部分です。私はテストを実行するたびにJFrame全体を起動するのに最適な解決策ではないと思うので、テストダブルを作成するにはどのようにすればよいでしょうか?再描画機能全体を壊すことはありません。
ありがとうございました!