简体   繁体   English

使用JUnit在Eclipse中运行测试用例

[英]Running test cases in Eclipse using JUnit

I'm trying to run a test using JUnit in Eclipse but the test case is not running and the holly console not showing anything. 我正在尝试在Eclipse中使用JUnit运行测试,但测试用例没有运行,而冬青控制台没有显示任何内容。 The only line appears in console is : 控制台中唯一出现的行是:

<terminated>AccountManagmentModuleTest[JUnit] D:\Program Files\Java\jdk1.6.0_26\bin\javaw.exe(Nov 23, 2012 12:08:49 PM)

All I want to do is run the test case. 我想做的就是运行测试用例。 Some lines are executing, like starting to connect to db, but no connection object created using DriverManager, also don't throw any exception. 有些行正在执行,比如开始连接到db,但没有使用DriverManager创建的连接对象,也不会抛出任何异常。

enter code here

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            conn = DriverManager.getConnection("jdbc:oracle:oci8:@local ip:1521:orcl", "dipak1","dipak1"); //got to finally from here, not even catching anything!!!
            cstmt = conn.prepareCall(query);  
            cstmt.execute();

        } catch (Exception e) {

            e.printStackTrace();
        } finally {

            if (null != conn) {

                if (null != cstmt) {
                    cstmt.close();

                }
                conn.close();
            }

        }

It sounds like your test is executing but not producing the outputs you expect. 听起来你的测试正在执行,但没有产生你期望的输出。 Ensure you have the JUnit view open: 确保打开JUnit视图:

Window > Show View > Other... > Java > JUnit 窗口>显示视图>其他...> Java> JUnit

This view will show you visually how many tests have run and how many have passed. 此视图将直观地显示已运行的测试数量以及已通过的测试数量。 If the tests are failing, you can right-click on the failed tests and debug them. 如果测试失败,您可以右键单击失败的测试并进行调试。 You can also see the exceptions that have occurred. 您还可以查看已发生的例外情况。

The Console window won't show you any output from your JUnit tests unless you've including output statements in your test (either through a logging framework or simple println statements). 除非您在测试中包含输出语句(通过日志记录框架或简单的println语句),否则Console窗口不会显示JUnit测试的任何输出。 Only the JUnit view will show you whether your tests passed. 只有JUnit视图才会显示您的测试是否通过。

When you doing Unit testing with Junit, you should not use try and catch block use throws Exception. 使用Junit进行单元测试时,不应该使用try和catch块使用throws Exception。 Instead of if (null != conn) use JUnit assertion testing (assertNotNull or assertEquals or another one). 而不是if(null!= conn)使用JUnit断言测试(assertNotNull或assertEquals或另一个)。 To run the JUNit test on Eclipse : 要在Eclipse上运行JUNit测试:

  1. Window > Show View > Other... > Java > JUnit 窗口>显示视图>其他...> Java> JUnit
  2. Right clic on the java file > Run as > JUnit test. 在java文件上右键单击>运行方式> JUnit测试。
  3. On the panel JUnit you can see the unit testing result is succes or failed. 在面板JUnit上,您可以看到单元测试结果是成功还是失败。

    import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;

    import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;

    import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

    import org.junit.Test; import org.junit.Test;

     @Test public void testDbConnexion() throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); conn = DriverManager.getConnection("jdbc:oracle:oci8:@local ip:1521:orcl", "dipak1","dipak1"); //got to finally from here, not even catching anything!!! cstmt = conn.prepareCall(query); cstmt.execute(); assertNotNull(conn); assertNotNull(cstmt); connn.close(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM