JUnit测试通过,但控制台Console中有报错:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

      有一次写项目,当我建好JUnit的测试类运行后,看到JUnit测试通过,但控制台Console中有报错。
      我发现出现了数据库加载驱动器异常,想到应该是mysql-connector-java-5.1.40-bin.jar包的问题,但我发现我已经将jar包放到WEB-INF/lib目录下了,想到了jar包放到WEB-INF/lib 下与放到Referenced Libraries下可能有不同。
      当时出错的错误代码是:


java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
     at java.net.URLClassLoader.findClass(Unknown Source)
     at java.lang.ClassLoader.loadClass(Unknown Source)
     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
     at java.lang.ClassLoader.loadClass(Unknown Source)
     at java.lang.Class.forName0(Native Method)
     at java.lang.Class.forName(Unknown Source)
     at com.ruanko.util.DBUtil.(DBUtil.java:20)
     at com.ruanko.dao.impl.UserDaoImpl.save(UserDaoImpl.java:89)
     at com.ruanko.dao.impl.UserDaoImplTest.testSave(UserDaoImplTest.java:27)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
驱动加载失败!
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/shortrentdb? useUnicode = true&characterEncoding = utf8
     at java.sql.DriverManager.getConnection(Unknown Source)
     at java.sql.DriverManager.getConnection(Unknown Source)
     at com.ruanko.util.DBUtil.getConnection(DBUtil.java:37)
     at com.ruanko.dao.impl.UserDaoImpl.save(UserDaoImpl.java:89)
     at com.ruanko.dao.impl.UserDaoImplTest.testSave(UserDaoImplTest.java:27)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
用户注册:insert into t_user(name,password,mobile,email)values(?,?,?,?)
java.lang.NullPointerException
     at com.ruanko.dao.impl.UserDaoImpl.save(UserDaoImpl.java:94)
     at com.ruanko.dao.impl.UserDaoImplTest.testSave(UserDaoImplTest.java:27)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
系统异常!
com.ruanko.util.AppException: Dtail message:0com.ruanko.dao.impl.UserDaoImpl.save
     at com.ruanko.dao.impl.UserDaoImpl.save(UserDaoImpl.java:112)
     at com.ruanko.dao.impl.UserDaoImplTest.testSave(UserDaoImplTest.java:27)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

     于是我向项目中添加该jar包:右击项目名称→Build Path→Configure Build Path...→Add External JARs...然后选择准备好的jar包
     我又一次运行测试文件:这次也是看到JUnit测试通过,控制台Console中有报错 。
     错误代码如下:


com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
     at java.lang.reflect.Constructor.newInstance(Unknown Source)
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
     at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
     at com.mysql.jdbc.MysqlIO.(MysqlIO.java:341)
     at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
     at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
     at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
     at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:806)
     at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
     at java.lang.reflect.Constructor.newInstance(Unknown Source)
     at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
     at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
     at java.sql.DriverManager.getConnection(Unknown Source)
     at java.sql.DriverManager.getConnection(Unknown Source)
     at com.ruanko.util.DBUtil.getConnection(DBUtil.java:37)
     at com.ruanko.dao.impl.UserDaoImpl.save(UserDaoImpl.java:89)
     at com.ruanko.dao.impl.UserDaoImplTest.testSave(UserDaoImplTest.java:27)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.ConnectException: Connection refused: connect
     at java.net.DualStackPlainSocketImpl.connect0(Native Method)
     at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
     at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
     at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
     at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
     at java.net.PlainSocketImpl.connect(Unknown Source)
     at java.net.SocksSocketImpl.connect(Unknown Source)
     at java.net.Socket.connect(Unknown Source)
     at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
     at com.mysql.jdbc.MysqlIO.(MysqlIO.java:300)
     ... 41 more
用户注册:insert into t_user(name,password,mobile,email)values(?,?,?,?)
java.lang.NullPointerException
     at com.ruanko.dao.impl.UserDaoImpl.save(UserDaoImpl.java:94)
     at com.ruanko.dao.impl.UserDaoImplTest.testSave(UserDaoImplTest.java:27)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
系统异常!
com.ruanko.util.AppException: Dtail message:0com.ruanko.dao.impl.UserDaoImpl.save
     at com.ruanko.dao.impl.UserDaoImpl.save(UserDaoImpl.java:112)
     at com.ruanko.dao.impl.UserDaoImplTest.testSave(UserDaoImplTest.java:27)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
     at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
     at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
     at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
     at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
 
嘿嘿,连接失败,才发现自己刚把数据库关了,于是打开!
          普天同庆!运行成功!





你可能感兴趣的:(JUnit异常,单元测试工具,数据库连接异常,控制台,JUnit,jar包)