Create a gist now

Instantly share code, notes, and snippets.

anonymous /gist:170190
Created Aug 19, 2009

What would you like to do?
Mockito unittest for Apache iBatis enum type handler
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.sql.SQLException;
import java.sql.Types;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
public class EnumTypeHandlerImplTest {
enum EnumType implements ValueEnum {
TYPE_1
, TYPE_2 ("Тип 2 (по-умолчанию)")
, TYPE_3 ("Тип 3 (по-умолчанию)", "Третий тип");
private final String defaultValue;
private final String[] otherValues;
private EnumType() {
this(null, new String[] {});
}
private EnumType(String defaultValue) {
this(defaultValue, new String[] {});
}
private EnumType(String defaultValue, String ... otherValues) {
this.defaultValue = defaultValue;
this.otherValues = otherValues;
}
@Override public String getDefaultValue() {
return defaultValue;
}
@Override public String[] getOtherValues() {
return otherValues;
}
}
@EnumTypeHandler(enumClass = EnumType.class, jdbcType = Types.VARCHAR)
class PaymentTypeHandler extends EnumTypeHandlerImpl {
}
@Mock ResultGetter resultGetter;
@Mock ParameterSetter parameterSetter;
PaymentTypeHandler handler = new PaymentTypeHandler();
{
MockitoAnnotations.initMocks(this);
}
@Test public void testGetResultForType1() throws SQLException {
when(resultGetter.wasNull()).thenReturn(false);
when(resultGetter.getObject()).thenReturn(EnumType.TYPE_1.name());
assertEquals(EnumType.TYPE_1, handler.getResult(resultGetter));
}
@Test public void testGetResultForType2() throws SQLException {
when(resultGetter.wasNull()).thenReturn(false);
when(resultGetter.getObject()).thenReturn(EnumType.TYPE_2.getDefaultValue());
assertEquals(EnumType.TYPE_2, handler.getResult(resultGetter));
}
@Test public void testGetResultForType3() throws SQLException {
when(resultGetter.wasNull()).thenReturn(false);
when(resultGetter.getObject()).thenReturn(EnumType.TYPE_3.otherValues[0]);
assertEquals(EnumType.TYPE_3, handler.getResult(resultGetter));
}
@Test public void testSetParameterForType1() throws SQLException {
handler.setParameter(parameterSetter, EnumType.TYPE_1);
verify(parameterSetter).setObject(EnumType.TYPE_1.name(), Types.VARCHAR);
}
@Test public void testSetParameterForType2() throws SQLException {
handler.setParameter(parameterSetter, EnumType.TYPE_2);
verify(parameterSetter).setObject(EnumType.TYPE_2.getDefaultValue(), Types.VARCHAR);
}
@Test public void testSetParameterForType3() throws SQLException {
handler.setParameter(parameterSetter, EnumType.TYPE_3);
verify(parameterSetter).setObject(EnumType.TYPE_3.getDefaultValue(), Types.VARCHAR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment