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)

解决方案 »

  1.   

    java.lang.Exception: Method getConnection() should be void给点源码啊 
      

  2.   

    getConnection必须无返回值,也就是声明为void,否则不能使用junit test运行。
      

  3.   

    @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;
          }
      

  4.   

    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的测试方法和一般的方法分开,写在另外一个测试类里
      

  5.   

    好吧,上边有点错误@Test
    public void getConnectionTest(){
         try{
               Connection conn= getConnection();
               Assert.assertTrue("连接数据库错误",  conn!=null);
         }
    ......
      

  6.   

    把@Test去掉就可以了!!!
      

  7.   

    困扰我2小时。。把2份方法放在一起能运行。差点以为Connection对象不能有返回值。
      

  8.   

    困扰我2小时。。把2份方法放在一起能运行。差点以为Connection对象不能有返回值。

    感谢,,去掉getConnection的 @Test就能运行了
    困扰我2小时。。把2份方法放在一起能运行。分开就不行,差点以为Connection对象不能有返回值。