用Jdbc连接数据库时候,出现了一个异常,实在无能力解决,来这里请教大神

ZXH15179 2015-04-18 05:34:45
java.lang.Exception: Method getConnection() should be void
at org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:97)
at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:70)
at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:133)
at org.junit.runners.BlockJUnit4ClassRunner.validateTestMethods(BlockJUnit4ClassRunner.java:186)
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:166)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:104)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:31)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestMethodReference.<init>(JUnit4TestMethodReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:54)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
...全文
479 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hsdmslh 2019-04-18
  • 打赏
  • 举报
回复
引用 8 楼 hsdmslh 的回复:
[quote=引用 7 楼 sxhe2010 的回复:] 把@Test去掉就可以了!!!
困扰我2小时。。把2份方法放在一起能运行。差点以为Connection对象不能有返回值。 [/quote] 感谢,,去掉getConnection的 @Test就能运行了 困扰我2小时。。把2份方法放在一起能运行。分开就不行,差点以为Connection对象不能有返回值。
hsdmslh 2019-04-18
  • 打赏
  • 举报
回复
引用 7 楼 sxhe2010 的回复:
把@Test去掉就可以了!!!
困扰我2小时。。把2份方法放在一起能运行。差点以为Connection对象不能有返回值。
sxhe2010 2015-09-18
  • 打赏
  • 举报
回复
把@Test去掉就可以了!!!
youzi05 2015-04-18
  • 打赏
  • 举报
回复
好吧,上边有点错误

@Test
public void getConnectionTest(){
     try{
           Connection conn= getConnection();
           Assert.assertTrue("连接数据库错误",  conn!=null);
     }
......
youzi05 2015-04-18
  • 打赏
  • 举报
回复
lz应该这样写:

@public void getConnectionTest(){
     try{
           Connection conn= getConnection();
           Assert.assertTrue("连接数据库错误",  conn!=null);
     }
     catch( Exception e ){
               fail(e.toString());
      }
      finally{
        if( conn != null)
         conn.close();			
    }
}

public Connection getConnection() throws Exception{
    	
    	String driverClass = null;
    	String jdbcUrl = null;
    	String user = null;
    	String password = null;
.....
}
另外建议lz的测试方法和一般的方法分开,写在另外一个测试类里
ZXH15179 2015-04-18
  • 打赏
  • 举报
回复
获取数据库连接,怎么可能会没有返回值呢??
ZXH15179 2015-04-18
  • 打赏
  • 举报
回复
@Test public Connection getConnection() throws Exception{ String driverClass = null; String jdbcUrl = null; String user = null; String password = null; InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(in); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("jdbcUrl"); user = properties.getProperty("user"); password = properties.getProperty("password"); Driver driver = (Driver) Class.forName(driverClass).newInstance(); Properties info = new Properties(); info.put(user, "user"); info.put(password, "password"); Connection connection = driver.connect(jdbcUrl, info); return connection; }
rumlee 2015-04-18
  • 打赏
  • 举报
回复
getConnection必须无返回值,也就是声明为void,否则不能使用junit test运行。
姜小白- 2015-04-18
  • 打赏
  • 举报
回复
java.lang.Exception: Method getConnection() should be void 给点源码啊

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧