[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-16
4
Medium Priority
?
505 Views
Last Modified: 2013-11-24
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

0
Comment
Question by:su2117
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 20090655
>          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
 

Author Comment

by:su2117
ID: 20093687
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20093764
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
 

Author Comment

by:su2117
ID: 20095024
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question