Anonymous (owner)

Revisions

  • 8d5aed Tue Aug 18 22:40:52 -0700 2009
gist: 170190 Download_button fork
public
Description:
Mockito unittest for Apache iBatis enum type handler
Public Clone URL: git://gist.github.com/170190.git
Embed All Files: show embed
Java #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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);
}
}