Exception thrown by a TimerTask

3月 25, 2023 7:22:39 下午 org.fest.swing.monitor.ProtectingTimerTask handleException
警告: Exception thrown by a TimerTask
java.lang.reflect.InaccessibleObjectException: Unable to make field int java.util.TimerTask.state accessible: module java.base does not "opens java.util" to unnamed module @2077d4de

img


idea2022
jdk17
FEST-Swing结合JUnit自动化测试
测试用例代码:

@Test
    public void testValidCompany() {

        /*
         * 第一个测试用例,测试:航空公司名称的长度不能小于6
         * 注意:其它输入项必须正确,否则,如何测试ValidCompany
         */
        jfDlg.textBox("txtIDFest").setText("93687");
        jfDlg.textBox("txtFnameFest").setText("YE4002");
        jfDlg.textBox("txtCompanyFest").setText("sdfaa");
        jfDlg.textBox("txtPriceFest").setText("  ");
        jfDlg.textBox("txtSeatFest").setText("126");
        jfDlg.button("btnOKFest").click();
        try {
            jfDlg.optionPane().okButton().click();
        }catch (org.fest.swing.exception.WaitTimedOutError ignored){}
        assertFalse(hif.GetValidFlight());

        /*
         * 第二个测试用例,测试:航空公司名称的长度不能大于20
         * 注意:其它输入项必须正确,否则,如何测试ValidCompany
         */
        jfDlg.textBox("txtIDFest").setText("93687");
        jfDlg.textBox("txtFnameFest").setText("YE4002");
        jfDlg.textBox("txtCompanyFest").setText("abcdeabcdeabcdeabcdea");
        jfDlg.textBox("txtPriceFest").setText("1500.5");
        jfDlg.textBox("txtSeatFest").setText("126");
        jfDlg.button("btnOKFest").click();
        try {
            jfDlg.optionPane().okButton().click();
        }catch (org.fest.swing.exception.WaitTimedOutError ignored){}
        assertFalse(hif.GetValidFlight());

        /*
         * 第三个测试用例,测试:航空公司名称的首字母必须为‘H’
         * 注意:其它输入项必须正确,否则,如何测试ValidCompany
         */
        jfDlg.textBox("txtIDFest").setText("93687");
        jfDlg.textBox("txtFnameFest").setText("YE4002");
        jfDlg.textBox("txtCompanyFest").setText("hguohang");
        jfDlg.textBox("txtPriceFest").setText("1500.5");
        jfDlg.textBox("txtSeatFest").setText("126");
        jfDlg.button("btnOKFest").click();
        try {
            jfDlg.optionPane().okButton().click();
        }catch (org.fest.swing.exception.WaitTimedOutError ignored){}
        assertFalse(hif.GetValidFlight());

        /*
         * 第四个测试用例,测试:航空公司名称的首字母必须为‘H’
         * 注意:其它输入项必须正确,否则,如何测试ValidCompany
         */
        jfDlg.textBox("txtIDFest").setText("93687");
        jfDlg.textBox("txtFnameFest").setText("YE4002");
        jfDlg.textBox("txtCompanyFest").setText("Hguohang");
        jfDlg.textBox("txtPriceFest").setText("1500.5");
        jfDlg.textBox("txtSeatFest").setText("126");
        jfDlg.button("btnOKFest").click();
        try {
            jfDlg.optionPane().okButton().click();
        }catch (org.fest.swing.exception.WaitTimedOutError ignored){}
        assertTrue(hif.GetValidFlight());
    }

求解决方案

把你项目中的module-info.java文件删掉

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以看下这个问题的回答https://ask.csdn.net/questions/655015
  • 你也可以参考下这篇文章:java.lang.RuntimeException的直接子类有这些
  • 除此之外, 这篇博客: 【Java基础】使用Junit5进行单元测试中的 8.超时操作的测试:assertTimeoutPreemptively 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    当我们希望测试耗时方法的执行时间,并不想让测试方法无限地等待时,就可以对测试方法进行超时测试,JUnit 5 对此推出了断言方法 assertTimeout,提供了对超时的广泛支持。

    假设我们希望测试代码在一秒内执行完毕,可以写如下测试用例

    @Test
    @DisplayName("超时方法测试")
    void test_should_complete_in_one_second() {
      Assertions.assertTimeoutPreemptively(Duration.of(1, ChronoUnit.SECONDS), () -> Thread.sleep(2000));
    }
    

    这个测试运行失败,因为代码执行将休眠两秒钟,而我们期望测试用例在一秒钟之内成功。但是如果我们把休眠时间设置一秒钟,测试仍然会出现偶尔失败的情况,这是因为测试方法执行过程中除了目标代码还有额外的代码和指令执行会耗时,所以在超时限制上无法做到对时间参数的完全精确匹配


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^