如何打印我的自定义错误消息而不通过try catch块

时间:2016-05-11 04:21:55

标签: java testng

@Test(dataProvider = "FileDataProvider2") public void testModifiedFiles(String excelData2, String csvData)throws Exception { try{ List<List<String>> excelList2 = ReadFromFile.readexcelFile(excelData2); List<List<String>> csvList1 = ReadFromFile.readCSVFile(csvData); logger.info("Test by Missing some data"); Assert.assertEquals(excelList2, csvList1); } catch(Throwable e){ Assert.fail("Data Misseddddddddddd..............", e); } }

  

2016-05-11 00:17:35 INFO TestClassToCompare:72 - 缺少测试   一些数据[Utils]试图创建   D:\ workspace \ ReadExcelCSVFiles \ test-output \ Default suite \ Default   test.html [Utils]目录   D:\ workspace \ ReadExcelCSVFiles \ test-output \默认套件存在:true   [Utils]试图创造   D:\ workspace \ ReadExcelCSVFiles \ test-output \ Default suite \ Default   test.xml [Utils]目录   D:\ workspace \ ReadExcelCSVFiles \ test-output \默认套件存在:true   FAILED:testModifiedFiles(“C:/ Users / leela   krishna / Desktop / Xlsx / company_modified.xlsx“,”C:/ Users / leela   krishna / Desktop / CSV / companies.csv“)java.lang.AssertionError:Data   Missedddddddddddd .............. at   org.testng.Assert.fail(Assert.java:83)at   com.qpair.readfile.ReadExcelCSVFiles.TestClassToCompare.testModifiedFiles(TestClassToCompare.java:76)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)     在org.testng.internal.Invoker.invokeMethod(Invoker.java:646)at   org.testng.internal.Invoker.invokeTestMethod(Invoker.java:823)at at   org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)at at   org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)     在   org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)     在org.testng.TestRunner.privateRun(TestRunner.java:778)at   org.testng.TestRunner.run(TestRunner.java:632)at   org.testng.SuiteRunner.runTest(SuiteRunner.java:366)at   org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)at at   org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)at at   org.testng.SuiteRunner.run(SuiteRunner.java:268)at   org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at   org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at   org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)at at   org.testng.TestNG.runSuitesLocally(TestNG.java:1150)at at   org.testng.TestNG.runSuites(TestNG.java:1075)at   org.testng.TestNG.run(TestNG.java:1047)at   org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)引起   by:java.lang.AssertionError:列表没有预期的相同大小   [67]但在org.testng.Assert.fail(Assert.java:94)处找到[66]   org.testng.Assert.failNotEquals(Assert.java:513)at   org.testng.Assert.assertEqualsImpl(Assert.java:135)at   org.testng.Assert.assertEquals(Assert.java:116)at   org.testng.Assert.assertEquals(Assert.java:389)at   org.testng.Assert.assertEquals(Assert.java:556)at   org.testng.Assert.assertEquals(Assert.java:533)at   com.qpair.readfile.ReadExcelCSVFiles.TestClassToCompare.testModifiedFiles(TestClassToCompare.java:73)     ......还有25个

     

===============================================       默认测试

     

测试运行:1,失败:1,跳过:0

     

===============================================默认套件总测试运行:1,失败:1,跳过:0   ===============================================`

2 个答案:

答案 0 :(得分:0)

在Java中,不要打印总异常“e”,只需尝试打印一次只会导致异常。

    System.out.println(e.getCause());

谢谢你, 穆拉利

答案 1 :(得分:0)

如果您想要弹出窗口,则可以使用

        JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());

OR

        System.out.println(ex.getMessage() + "My Custom error");

演示如下: -

public static void main(String[] args) {
    try {
        throw new RuntimeException("Cannot move file");
    } catch (Exception ex) {
        //JOptionPane.showMessageDialog(null, "Error: " + ex.getMessage());
        System.out.println(ex.getMessage() + "My Custom error");
    }
}

希望它会对你有所帮助:)。