- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 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 传递到我的 View 。 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 时,它工作得很好,我所有的形状都被绘制了。我在测试设置中遗漏了什么吗?
此外,这可能是最重要的部分,我想每次运行测试时启动整个 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);
}
}
关于java - 如何创建支持重绘功能的 JFrame/JPanel 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41077841/
我正在尝试使用 NetBeans 在 Java 中制作类似幻灯片的应用程序。 我有一个 JFrame(主窗口),里面有两个用于导航的按钮(后退和下一步),还有一个 JPanel(mainPanel),
新代码 package test; import javax.swing.*; import java.awt.*; public class TestWindow extends JFrame{ /
我是 Java swing 编码新手。我正在尝试将 JPanel 内容复制到一个新的 JPanel,它使用原始 JPanel 的内容来显示。此外,原始 JPanel 内容随着记录的变化而变化。我尝试了
我正在尝试创建一个带有另外两个 JPanel 的 JPanel,但是,当时,当我要显示主要内容时JPanel,它只显示主Jpanel上的第一个Jpanel。 通过以下示例,您将更好地理解这个问题: 我
我正在尝试学习如何使用编码风格在 Java 中完成 GUI 的工作这就是我写的: import java.awt.Container; import java.awt.Panel; import ja
我从 Oracle 教程中得到了一个 JPanel 的例子我看到它使用默认方法关闭窗口 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 我想
我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗? BibP() setLayout(new
我试图让一个 JPanel 出现在另一个 JPanel 中。目前,JPanel 位于外部 JFrame 中,并与我的其他 JFrame 一起加载。我希望 JPanel 位于另一个 JPanel 内部,
是否可以在 Java 的一个方法中在一个面板中包含一个面板? 就像,如果我在名为 CreatePanel 的方法中创建了一个 JPanel,我可以在其下面添加另一个吗?我正在尝试在一种方法中添加两个或
我对嵌套的 BoxLayouts 有疑问。 我想构建一个由 2 个子面板组成的 DropDownPanel:顶部的标题和底部的主体。 body 最初是隐藏的。通过单击标题,您可以切换正文的可见性并显示
我需要创建一个在 JFrame 或 JPanel 上显示多个矩形的程序。这是我到目前为止提出的代码: import javax.swing.*; import java.util.Random; im
将站点中的一些图像保存到 ArrayList 后,我试图创建一个 jpanel,它将在带有滚动 Pane 的单独 jpanel 中显示所有这些图像,以便我可以向每个图像添加 Action 事件。然
我正在开发一个简单的注册窗口,它会在 Java 应用程序打开时出现。 这是一个 JFrame,里面有一个 JPanel,它有文本字段、标签,另一个面板也包含文本字段和标签。我的问题是外部面板有背景图像
我有这个界面要创建。我有 JScrollPane 的问题: 我声明了一个带有 Gridlayout(8,1,0,2) 的 JPanel,我希望在这个面板中出现 8 行。 一行是一个JPanel,我设置
我已经创建了 JFreeChart 并将其放入图表面板(按照建议)。我还将它添加到 jPanel 中。我正在使用 jFrame。但是运行程序后我的图表不可见。有谁能帮帮我吗? final JFreeC
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel
我想从窗口 (JFrame) 中删除旧的 JPanel 并添加一个新的。我该怎么做呢? 我尝试了以下方法: public static void showGUI() { JFrame fram
我刚刚接触 Java,正在为我的大学类(class)开发一个项目。我正在开发一款《百万富翁》游戏,但我陷入困境。 我有一个 JFrame 类,其中有 2 个面板。第一个是由按钮组成的,第二个是我想通过
所以,我正在制作一个绘画程序,我有一个主要的Paint类,它检测鼠标输入和绘画,还有一个Tools类,它是左侧的工具栏,拥有许多工具,例如画笔大小更改和形状更改。因此,我想向 Tools 类添加一个清
我正在尝试制作一个在 JToggleButton 的帮助下激活的弹出面板。我希望在选择 ToggleButton 时将 JPanel 添加到另一个 Jpanel 上,并在取消选择 ToggleButt
我是一名优秀的程序员,十分优秀!