Results 1 to 4 of 4
  1. #1
    FIREDANCEXS is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default JUnit help please

    Please guys, can I have some help with JUnit testing.

    I have a Java class called Cash that updates data in a database as so. This works fine as I use the method from a web interface with a servlet. The connection etc is established in the constructor so that isnt a worry.

    Java Code:
        public void setCash(String user, String cash) {
            try {
                Statement s = null;
                s = conn.createStatement();
                String sqlCode = "UPDATE cash_table set cash='" + cash+ "' where user='" + user+ "'";
                s.executeUpdate(sqlCode);
                s.close();
            } catch (Exception e) {
            }
        }
    Now, I am trying to create a JUNIT test that will call this method using the following code but it doesnt work in NetBeans. The method setCash is called but the database doesnt update? Why???

    Java Code:
    package ModelTests;
    import model.Cash;
    import org.junit.Before;
    import org.junit.Test;
    import junit.framework.TestCase;
    
    public class CashTest extends TestCase {    
        private Cash cash;
                
        public CashTest() {
        }
        
        @Before
        public void setUp() throws Exception {
            cash= new Cash();
        }
    
         @Test
         public void testSetTarget() {
         cash.setCash("frank", "400");        
         }
        
    }
    Last edited by Fubarable; 04-24-2012 at 10:57 PM. Reason: "Urgent" removed

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: JUnit help please

    How do you setup / obtain your connection object in the constructor?

  3. #3
    FIREDANCEXS is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: JUnit help please

    As so:

    InitialContext icon = new InitialContext();
    DataSource datasource = (DataSource) icon.lookup("java:comp/env/jdbc/mydatabase");
    conn = datasource.getConnection();

    I put some print statements in and found anything after this line isnt called???
    s = conn.createStatement();

    Why on earth... :S

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: JUnit help please

    I think it is because you unit test doesn't have the connection to the database. Using the code above will only allow you to get the connection when the application is deployed on an application server where it can look-up for a datasource.

    The line after the code you mentioned above is a sign that some error was thrown there. I believe the the connection object is null because it cannot obtain the resource from outside the web application context. So it is better to setup the connection object inside your setup method in your unit test.

Similar Threads

  1. junit 4 QTP
    By limcy in forum Advanced Java
    Replies: 0
    Last Post: 09-26-2011, 07:20 AM
  2. Need Help In JUnit
    By javajee in forum New To Java
    Replies: 5
    Last Post: 08-09-2011, 07:58 PM
  3. JUnit
    By cka in forum Eclipse
    Replies: 3
    Last Post: 07-27-2010, 04:14 PM
  4. [URGENT]SHA Encryption System...need urgent helps
    By java_idiot in forum New To Java
    Replies: 6
    Last Post: 05-02-2010, 10:04 AM
  5. Junit
    By Azndaddy in forum New To Java
    Replies: 6
    Last Post: 06-15-2008, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •