12 Replies Latest reply on Aug 11, 2011 8:04 AM by Kayaman

    how to fill ResultSet  ?

    user575089
      I have written a select query which hits the DB and fetches results. my DAO layer populates the data in a VO. However , there is no data present in DB now. I wish to mock the resultset so that I get the populated VO in the presentation layer to display a table in JSP.

      How do I mock a java.sql.ResultSet and fill the Column and data into it ?

      Is there any way I can inject mock data into resultset like below in DAO layer ?

      rs.addColumn("Column1",val1);
      rs.addColumn("Column2",val2);
      rs.addColumn("Column3",val3);

      I have checked this http://mockrunner.sourceforge.net/ library but they dont use java.sql.ResultSet.


      What are other possible ways I can think of to mock my DAO layer and test the presentation layer ?
        • 1. Re: how to fill ResultSet  ?
          836548
          user575089 wrote:
          I have written a select query which hits the DB and fetches results. my DAO layer populates the data in a VO. However , there is no data present in DB now. I wish to mock the resultset so that I get the populated VO in the presentation layer to display a table in JSP.

          How do I mock a java.sql.ResultSet and fill the Column and data into it ?

          Is there any way I can inject mock data into resultset like below in DAO layer ?

          rs.addColumn("Column1",val1);
          rs.addColumn("Column2",val2);
          rs.addColumn("Column3",val3);

          I have checked this http://mockrunner.sourceforge.net/ library but they dont use java.sql.ResultSet.


          What are other possible ways I can think of to mock my DAO layer and test the presentation layer ?
          You can use insertRow() method, for detailed explaination, refer below link:

          http://download.oracle.com/javase/6/docs/api/java/sql/ResultSet.html
          • 2. Re: how to fill ResultSet  ?
            user575089
            Thanks for the link

            I have two issues.

            (1) The link you sent for jdk 6 ..I'm using jdk 1.5

            (2) I'm using preparedstatement . The code needs this way ..

            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);


            However , I have.....
            pstmt = conn.prepareStatement(SELECT_QUERY_TASK);


            Is it still possible ?
            • 3. Re: how to fill ResultSet  ?
              836548
              user575089 wrote:
              Thanks for the link

              I have two issues.

              (1) The link you sent for jdk 6 ..I'm using jdk 1.5
              It is there in 1.5 also.
              (2) I'm using preparedstatement . The code needs this way ..

              stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);


              However , I have.....
              pstmt = conn.prepareStatement(SELECT_QUERY_TASK);

              You can use it in preparedStatement also

              pstmt = conn.prepareStatement(SELECT_QUERY_TASK,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

              Is it still possible ?
              • 4. Re: how to fill ResultSet  ?
                Kayaman
                The insertRow method inserts a row into the resultset and into the database. Why not just fill the database with test data? It seems like it would be the easiest way.
                • 5. Re: how to fill ResultSet  ?
                  836548
                  Kayaman wrote:
                  The insertRow method inserts a row into the resultset and into the database. Why not just fill the database with >test data? It seems like it would be the easiest way.
                  Yes very true, I think we have suggested both the ways, now its OP turn.
                  if he want to insert the rows in DB only if the table is empty, then he may go for this solution (insertRow) and ZAP (delete rows) all the tables once the activity is finished.
                  • 6. Re: how to fill ResultSet  ?
                    user575089
                    Why not just fill the database with test data? It seems like it would be the easiest way.
                    because I dont want to auto increment the serial numbers and waste sequence numbers of these tables . I dont want to reset all these staff again.

                    I have couple of tables for which I would like to mock this. the data will be filled up by a store procedure from external database manually....however,that process will take some time.In between , I would like to mock the resultset and testify the presentation layer.
                    • 7. Re: how to fill ResultSet  ?
                      user575089
                      The insertRow method inserts a row into the resultset and into the database.
                      damn!....I dont want data to be really inserted into database.

                      It seems there is no other tricks without filling the DB with test data :(
                      • 8. Re: how to fill ResultSet  ?
                        user575089
                        my idea was...even if the select query returns null records, I would INJECT some mock data into ResultSet so that rs.getString() works well .
                        • 9. Re: how to fill ResultSet  ?
                          Kayaman
                          user575089 wrote:
                          because I dont want to auto increment the serial numbers and waste sequence numbers of these tables.
                          Why, do you have a limited amount of sequence numbers? : )
                          I dont want to reset all these staff again.
                          With the time you've spent on this problem you would've had ample time to write two scripts, one to insert testing data in the database and the other to reset the database to its original state.

                          Or you could use a mocking framework, but you seemed to have trouble with it. And it's a lot clearer with the scripts.
                          • 10. Re: how to fill ResultSet  ?
                            darke
                            user575089 wrote:
                            The insertRow method inserts a row into the resultset and into the database.
                            damn!....I dont want data to be really inserted into database.

                            It seems there is no other tricks without filling the DB with test data :(
                            I believe http://mockrunner.sourceforge.net/ has a ResultSet implementation you can use .
                            • 11. Re: how to fill ResultSet  ?
                              Tolls
                              user575089 wrote:
                              Thanks for the link

                              I have two issues.

                              (1) The link you sent for jdk 6 ..I'm using jdk 1.5

                              (2) I'm using preparedstatement . The code needs this way ..

                              stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);


                              However , I have.....
                              pstmt = conn.prepareStatement(SELECT_QUERY_TASK);


                              Is it still possible ?
                              Where does the connection come from?
                              If you really want to avoid hitting the db then mock out the connection, statement and resultset.
                              They're all interfaces after all, so there must be a framework you can use to do that (possibly with a little modification of how you get hold of your connection).
                              • 12. Re: how to fill ResultSet  ?
                                Kayaman
                                Or use a different database (like hsqldb) for the testing.

                                Frankly this is an incredibly simple case to solve, and the main problem to me seems to be the developer. Can he be trusted to work with the database if he can't figure out something like this?