简体   繁体   中英

Test a method with JUnit

How can I test this method :

public void updateTable() {                 
    try {
        String sql = "select * from userDetails";
        rs = st.executeQuery(sql);
        st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        table.setModel(DbUtils.resultSetToTableModel(rs));  
    }
    catch(Exception e) {
    }

Few suggestions to make this more testable.

updateTable method is doing two things here.

  1. Execute query and get results
  2. Apply the result set to the table.

I would refactor to have two methods.

  1. public ResultSet getResultSetForQuery(String sql,Statement st)
  2. public Table updateTable(ResultSet rs)

Writing tests for the above two methods should be straightforward.

The two main points of any good test are:

  1. Check that it works well
  2. Localize the error if it's exist

Check that it works well Localize the error if it's exist In your case we cannot perform such a good testing to localize the error if it's exist because your method does too complex job. It wold be good to refactor your method into several methods to make it more readable and testable. I agree with @AjayGeorge about the way to separate that method.

And then you can write something like:

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.sql.ResultSet;

public class TestExample {

    @BeforeClass
    public static void setup() {
        // init your connection here
        // and insert data for tests
    }

    @AfterClass
    public static void cleanup() {
        // close connection
    }

    @Test
    public void testTableUpdate() {

        // initialize 'sqlQuery' and 'statement'

        ResultSet resultSet = getResultSetForQuery(sqlQuery, statement);

        // check 'resultSet' with JUnit methods like assertTrue

        updateTable(resultSet);

        // check that table is filled as you expected
    }

}

There are so many java libraries available to mock the large data to help testing database related methods. For example Mockito / EasyMock / JMock and more. You can make use of these libraries. You can mock the expected results using the tools and you can test your methods with the expected results.

JUnit: In general, you write a test class like the following and annotate the method(s) that contains you test with @Test. If you want to write a test that has to fail, you can use the 'expected' attribute of the annotation. If you know your test might be running for too long and want it to timeout after a certain period of time, use the 'timeout' attribute in the annotation.

If you have certain logic for initialization before each test method, you put that into another method and annotate that one with @Before. Likewise, to free stuff up, you use @After. For initialization that is to be run once per test class , use the annotation @BeforeClass and make sure that method is public and static - same story with @AfterClass.

In general, in each test method you go like this: Execute some of your code and then make assertions about what you would expect to be the case. In my example, I am testing the method 'myAdd' and I expect that 1+1 adds up to two 2.

public class MyTest {
  @Test
  public void testAddition() {
    int sum = myAdd(1, 1);
    assertEquals(2, sum);
  }
}

This example is based on JUnit: https://github.com/junit-team/junit/wiki There are alternatives, like TestNG: http://testng.org/doc/index.html

If you want to test the behaviour of a certain class in relation to its depdencies, a mock framework is recommended. Examples include: jmock, mockito, etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM