Question

I have used mybatis, and as part of this I have needed to write a custom handler object to convert from database TIMESTAMP to Java XMLGregorianCalendar object. After some testing, the conversion seems to work except it's not returning times correctly.

In order to ensure I get this running correctly and that I have tests to prove what can be converted I was trying to write a junit test to cover the conversion.

In the test, I have the following:

XMLGregorianCalendarHandler handler = new XMLGregorianCalendarHandler();

and I am trying to test that the getResults method using:

assertEquals(expectedDate , handler.getResult(rs, "timestamp"));

However, I'm not sure how to actually "create" a dummy ResultSet object to pass in as the 'rs' object so I can process the test.

Can anyone point me in the right direction please? am I even on the right track as to how I should be testing an object handler for mybatis?

The actual test code is:

@Test
public void testConvertTimestamp() throws SQLException, DatatypeConfigurationException{
    String dbTimestamp = "24-APR-14 15.47.44.000000000";

    XMLGregorianCalendar expectedDate = null;
    GregorianCalendar c = new GregorianCalendar();
    c.set(GregorianCalendar.DAY_OF_MONTH, 24);
    c.set(GregorianCalendar.MONTH, GregorianCalendar.APRIL);
    c.set(GregorianCalendar.YEAR, 2014);

    c.set(GregorianCalendar.HOUR_OF_DAY, 15);
    c.set(GregorianCalendar.MINUTE, 47);
    c.set(GregorianCalendar.SECOND, 44);
    c.set(GregorianCalendar.MILLISECOND, 000000000);

    expectedDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);        

    ResultSet rs;       

    assertEquals(expectedDate , handler.getResult(rs, RS_COLUMN_NAME));

}
Was it helpful?

Solution

Use any mock library to create resultset mock. In Mockito this can look like:

ResultSet rs = Mockito.mock(ResultSet.class);

Mockito.when(resultSetMock.next()).thenReturn(true).thenReturn(false);    
Mockito.when(resultSetMock.getString(RS_COLUMN_NAME)).thenReturn(dbTimestamp);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top