如何在Junit中测试JOptionPane show Message?

时间:2019-05-20 06:49:53

标签: java swing joptionpane testcase

我想验证Joptionpan是否正常工作,并且该消息是我从下面的示例代码中获得的消息。

示例Java代码:

MainUI.java

public MainUI extends JFrame {

    @Autowired
    private AnotherClass another;

    JPanel contentPane;

    public MainUI(){
        screenDrawing();
    }

    private void screenDrawing(){
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        this.setContentPane(contentPane);
        JButton btn = new JButton("test btn");
        contentPane.add(btn);
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                startValid();
            }
        });
    }

    private void startValid() {
        if(another.isTrue()){
             //do something
        }else{
            JOptionPane.showMessageDialog(contentPane, "Test message", "message", JOptionPane.NO_OPTION);
        }
    }
}

JoptionPane可以工作并在UI中表达,这也是一个问题。

示例Java测试代码:

MainUITest.java

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class MainUITest extends TestCase {
    @Autowired
    private MainUI mainUI;        

    @Autowired
    private AnotherClass another;

    @Test
    public void testInitializeUI() throws Exception {
        another.setIsTrue(false);
        mainUI.initializeUI(); 
        //How to test Joption show message
        assertEquals(????,????);
    }
}

0 个答案:

没有答案