Reputation: 61
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) {
}
Upvotes: 0
Views: 978
Reputation: 5142
The two main points of any good test are:
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
}
}
Upvotes: 0
Reputation: 8403
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
Upvotes: 0
Reputation: 1211
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.
Upvotes: 0
Reputation: 11875
Few suggestions to make this more testable.
updateTable
method is doing two things here.
I would refactor to have two methods.
public ResultSet getResultSetForQuery(String sql,Statement st)
public Table updateTable(ResultSet rs)
Writing tests for the above two methods should be straightforward.
Upvotes: 1