2016-12-10 9 views
1

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全体を起動するのに最適な解決策ではないと思うので、テストダブルを作成するにはどのようにすればよいでしょうか?再描画機能全体を壊すことはありません。

ありがとうございました!

答えて

0

私はあなたがそれゆえあなたはMockitoフレームワーク(またはその他)を使用して、これらの他の依存関係をモックする必要があり、あなたコードの代わりに、JPanelの実装をテストすることに集中すべきだと思う:

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); 
    } 
} 
関連する問題