簡體   English   中英

如何創建支持重繪功能的JFrame / JPanel的模擬

[英]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類基本上是Line類的裝飾器,它知道如何在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時,它的工作完全正常,我的所有Shapes都會被繪制。 我在測試設置中遺漏了什么嗎?

而且,這可能是最重要的部分,我想這並不是每次運行我的測試時啟動整個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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM