Test passes in JUnit however console has many error messages - How is this - what did I miss

I am new to both so Please take the time to explain (sorry I am a slow learner)
test case testpep3.java for class pep3.java (also uses a connection class)
package vgio4d10s;

import java.sql.ResultSet;
import java.sql.SQLException;

import junit.framework.TestCase;

public class Pep3Test extends TestCase {

      public void testMain() {

            VgiConnection conn = new VgiConnection();
                  assertEquals("testMain", this.getName());
                  ResultSet rec = null;
                  
      }
public void testGetPep(){
      VgiConnection conn = new VgiConnection();
      assertEquals("testGetPep", this.getName());
      ResultSet rec = null;
       try {
         rec = conn.select(
                 "SELECT FNM, LNM, MIN, TITLE, PEP_ID, MAX_EDU FROM pep where PEP_ID='10000000000000001001' " );
         System.out.println(rec.getString("LNM"));

   } catch (SQLException e) {
         System.out.println("SQL Error: " + e.toString() + " "
               + e.getErrorCode() + " " + e.getSQLState());e.printStackTrace();
   }
   
   
   
      }
}

_+_+_+_+_+_+_+_
Pep3.java
package vgio4d10s;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Pep3 {
      public static void main(String[] arguments) {
            VgiConnection conn2 = new VgiConnection();
            ResultSet rec = null;
           
                  try {
                        rec = conn2.select(
                                "SELECT FNM, LNM, MIN, TITLE, PEP_ID, MAX_EDU FROM pep " );

                  } catch (SQLException e) {
                        System.out.println("SQL Error: " + e.toString() + " "
                              + e.getErrorCode() + " " + e.getSQLState());e.printStackTrace();
                  }
                  System.out.println("First Name" + "/t"+"Last Name" + "/t" +"Middle Initial"+"/t" + "TITLE " + "/t" + "Max Education" + "/t" + "User ID People in PEP file ");
                 
                  try {
                        while(rec.next()) {
                            System.out.println(rec.getString("FNM")+  "\t"
                                        + " people ID"+ rec.getString("LNM")+  "\t"
                                    + rec.getString("MIN")+  "\t"
                                    + rec.getString("TITLE")+ " ANvarchar " + "\t"
                                    + rec.getString("PEP_ID")  + " Nick Name " +"\t"
                                    // the Field is a Nvarchar and I am not able display this on the console when
                                    // I run under open dialogue to the console
                                    + rec.getString("MAX_EDU")    );
                        }
                  } catch (SQLException e) {
                        System.out.println("SQL Error: " + e.toString() + " "
                              + e.getErrorCode() + " " + e.getSQLState());e.printStackTrace();
                             
                  }
                conn2.close();
                 
                 
      }

}
_+_+_+__+_+__
Console error
SQL Error: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state 0 24000
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
      at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
      at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
      at vgio4d10s.Pep3Test.testGetPep(Pep3Test.java:24)
      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 junit.framework.TestCase.runTest(TestCase.java:164)
      at junit.framework.TestCase.runBare(TestCase.java:130)
      at junit.framework.TestResult$1.protect(TestResult.java:106)
      at junit.framework.TestResult.runProtected(TestResult.java:124)
      at junit.framework.TestResult.run(TestResult.java:109)
      at junit.framework.TestCase.run(TestCase.java:120)
      at junit.framework.TestSuite.runTest(TestSuite.java:230)
      at junit.framework.TestSuite.run(TestSuite.java:225)
      at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



_+_+_
to see if the Pep3 can be run as I added a main object and it runs without a problem and displays the data


SO my question is if the junit test is passing then I am not testing where the failure is occurring or the test does not check all aspects
Please help and explain - all the books I have read (close 5 on this seem to indicate that if you test first the code has lesser chance of errors)
Thanks in advance

su2117Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
you need to call next() before you can get a value from a result set.

   } catch (SQLException e) {
         System.out.println("SQL Error: " + e.toString() + " "
               + e.getErrorCode() + " " + e.getSQLState());e.printStackTrace();
          fail("An exception was thrown");
   }


or alternatively don't catch the exception at all
0
 
objectsCommented:
>          System.out.println(rec.getString("LNM"));

should be:

if (rs.next()) System.out.println(rec.getString("LNM"));

>   } catch (SQLException e) {

If your catching an excepiton in a test then you might want to be failing the test if it gets thrown
0
 
su2117Author Commented:
It solved the problem but please explain why I need that in my test and how I should code the test so that it would " failing the test if it gets thrown" as you said in your comment earlier.
Thanks
Su2117
Here is what I have
package vgio4d10s;

import java.sql.ResultSet;
import java.sql.SQLException;

import junit.framework.TestCase;

public class Pep3Test extends TestCase {

      public void testMain() {

            VgiConnection conn = new VgiConnection();
                  assertEquals("testMain", this.getName());
                  ResultSet rec = null;
                  
      }
public void testGetPep(){
      VgiConnection conn = new VgiConnection();
      assertEquals("testGetPep", this.getName());
      ResultSet rec = null;
       try {
         rec = conn.select(
                 "SELECT FNM, LNM, MIN, TITLE, PEP_ID, MAX_EDU FROM pep where PEP_ID='10000000000000001001' " );
         if (rec.next()){ System.out.println(rec.getString("LNM"));

          }


   } catch (SQLException e) {
         System.out.println("SQL Error: " + e.toString() + " "
               + e.getErrorCode() + " " + e.getSQLState());e.printStackTrace();
   }
   
   
   
      }
}

As I stated earlier I am trying to learn the correct procedures so please take the time to explain
Thanks
0
 
su2117Author Commented:
Additionally I am trying to print all the values collected by the getPep method
Is that programatically wrong ? If not how should the statement be constructed and why ?
I tried the following
Added an Assert equals  and the system (eclipse)wanted to change it assert same as
So I removed it and as you see above it worked
I tried to add other Instance variables (is that the correct term) like the FNM which is part of the select statement and again the system thru a lot of errors on the console while the test itself passed (expected as there were no assert statements to actually check.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.