11package com .blogspot .ihaztehcodez .magic ;
22
3+ import com .blogspot .ihaztehcodez .magic .binders .VoidBinding ;
4+ import com .blogspot .ihaztehcodez .magic .utility .SimpleConnectionProvider ;
5+ import org .junit .Test ;
6+
37import java .sql .CallableStatement ;
48import java .sql .Connection ;
59import java .sql .SQLException ;
610import java .util .ArrayList ;
711import java .util .LinkedList ;
812import java .util .List ;
913
10- import org .junit .Test ;
11-
12- import com .blogspot .ihaztehcodez .magic .utility .SimpleConnectionProvider ;
13-
14- import static junit .framework .Assert .*;
15- import static org .easymock .EasyMock .*;
16-
14+ import static junit .framework .Assert .assertNull ;
15+ import static junit .framework .Assert .fail ;
16+ import static org .mockito .Mockito .*;
17+
1718public class TestSQLHandler {
1819 @ Test ( expected = NullPointerException .class )
1920 public void ctor_rejectsNullDatabase () {
@@ -22,117 +23,77 @@ public void ctor_rejectsNullDatabase() {
2223
2324 @ Test
2425 public void invoke_executesStatementGivenInAnnotation () throws Throwable {
25- Connection connection = createMock (Connection .class );
26- CallableStatement statement = createMock (CallableStatement .class );
27-
28- expect (connection .prepareCall ("SQL" ))
29- .andReturn (statement );
30- expect (statement .execute ()).andReturn (false );
26+ Connection connection = mock (Connection .class );
27+ CallableStatement statement = mock (CallableStatement .class );
3128
32- statement .close ();
33- connection .close ();
34-
35- Binding returnBinding = createMock (Binding .class );
36- expect (returnBinding .worksWith ()).andReturn (new Class <?>[] { void .class } );
37- returnBinding .prepareGet (statement , 1 );
38- expect (returnBinding .getValue (statement , 1 )).andReturn (null );
39- expect (returnBinding .parameters ()).andReturn (0 );
29+ when (connection .prepareCall ("SQL" ))
30+ .thenReturn (statement );
31+
32+ Binding returnBinding = new VoidBinding ();
4033 List <Binding > bindings = new LinkedList <Binding >();
4134 bindings .add (returnBinding );
42-
43- replay (connection , statement , returnBinding );
44-
35+
4536 SQLHandler subject = new SQLHandler (bindings , new SimpleConnectionProvider (connection ));
4637 assertNull (subject .invoke (null , Stubby .class .getMethod ("stubby" ), new Object [0 ]));
47-
48- verify (connection , statement , returnBinding );
38+
39+ verify (statement ). execute ( );
4940 }
5041
5142 @ Test
5243 public void invoke_passesOnVariables () throws Throwable {
53- Connection connection = createMock (Connection .class );
54- CallableStatement statement = createMock (CallableStatement .class );
55-
56- expect (connection .prepareCall ("SQL" ))
57- .andReturn (statement );
58- expect (statement .execute ()).andReturn (false );
59-
60- statement .close ();
61- connection .close ();
62-
44+ Connection connection = mock (Connection .class );
45+ CallableStatement statement = mock (CallableStatement .class );
46+
47+ when (connection .prepareCall ("SQL" ))
48+ .thenReturn (statement );
49+
6350 List <Binding > bindings = new LinkedList <Binding >();
6451
65- Binding returnBinding = createMock (Binding .class );
66- expect (returnBinding .worksWith ()).andReturn (new Class <?>[] { void .class } );
67- returnBinding .prepareGet (statement , 1 );
68- expect (returnBinding .parameters ()).andReturn (0 );
69- expect (returnBinding .getValue (statement , 1 )).andReturn (null );
52+ Binding returnBinding = new VoidBinding ();
7053 bindings .add (returnBinding );
7154
72- Binding paramiter = createMock (Binding .class );
73- expect (paramiter .worksWith ()).andReturn (new Class <?>[] { int .class } );
74- paramiter .setValue (statement , 1 , 8 );
75- expect (paramiter .parameters ()).andReturn (1 );
76- bindings .add (paramiter );
77-
78- replay (connection , statement , returnBinding , paramiter );
55+ Binding paramiter = mock (Binding .class );
56+ when (paramiter .worksWith ()).thenReturn (new Class <?>[]{int .class });
7957
58+ when (paramiter .parameters ()).thenReturn (1 );
59+ bindings .add (paramiter );
60+
8061 SQLHandler subject = new SQLHandler (bindings , new SimpleConnectionProvider (connection ));
81- assertNull (subject .invoke (null , Stubby .class .getMethod ("intIt" , new Class <?>[] { int .class } ),
62+ assertNull (subject .invoke (null , Stubby .class .getMethod ("intIt" , int .class ),
8263 new Object [] { 8 }));
83-
84- verify (connection , statement , returnBinding , paramiter );
64+
65+ verify (paramiter ). setValue ( statement , 1 , 8 );
8566 }
8667
8768 @ Test
8869 public void invoke_commitsOnRequest () throws Throwable {
89- Connection connection = createMock (Connection .class );
90- CallableStatement statement = createMock (CallableStatement .class );
91-
92- expect (connection .prepareCall ("SQL" ))
93- .andReturn (statement );
94- expect (statement .execute ()).andReturn (false );
95- connection .commit ();
96- statement .close ();
97- connection .close ();
70+ Connection connection = mock (Connection .class );
71+ CallableStatement statement = mock (CallableStatement .class );
72+
73+ when (connection .prepareCall ("SQL" ))
74+ .thenReturn (statement );
9875
99- Binding returnBinding = createMock (Binding .class );
100- expect (returnBinding .worksWith ()).andReturn (new Class <?>[] { void .class } );
101- returnBinding .prepareGet (statement , 1 );
102- expect (returnBinding .getValue (statement , 1 )).andReturn (null );
103- expect (returnBinding .parameters ()).andReturn (0 );
10476 List <Binding > bindings = new LinkedList <Binding >();
105- bindings .add (returnBinding );
106-
107- replay (connection , statement , returnBinding );
108-
77+ bindings .add (new VoidBinding ());
78+
10979 SQLHandler subject = new SQLHandler (bindings , new SimpleConnectionProvider (connection ));
11080 assertNull (subject .invoke (null , Stubby .class .getMethod ("commiting" ), new Object [0 ]));
11181
112- verify (connection , statement , returnBinding );
82+ verify (connection ). commit ( );
11383 }
11484
11585 @ Test
11686 public void invoke_rollsbackOnRequest () throws Throwable {
117- Connection connection = createMock (Connection .class );
118- CallableStatement statement = createMock (CallableStatement .class );
119-
120- expect (connection .prepareCall ("SQL" ))
121- .andReturn (statement );
122- expect (statement .execute ()).andThrow (new SQLException ());
123- connection .rollback ();
124- statement .close ();
125- connection .close ();
126-
127- Binding returnBinding = createMock (Binding .class );
128- expect (returnBinding .worksWith ()).andReturn (new Class <?>[] { void .class } );
129- returnBinding .prepareGet (statement , 1 );
130- expect (returnBinding .parameters ()).andReturn (0 );
87+ Connection connection = mock (Connection .class );
88+ CallableStatement statement = mock (CallableStatement .class );
89+
90+ when (connection .prepareCall ("SQL" ))
91+ .thenReturn (statement );
92+ when (statement .execute ()).thenThrow (new SQLException ());
93+
13194 List <Binding > bindings = new LinkedList <Binding >();
132- bindings .add (returnBinding );
133-
134- replay (connection , statement , returnBinding );
135-
95+ bindings .add (new VoidBinding ());
96+
13697 SQLHandler subject = new SQLHandler (bindings , new SimpleConnectionProvider (connection ));
13798 try {
13899 assertNull (subject .invoke (null , Stubby .class .getMethod ("commiting" ), new Object [0 ]));
@@ -141,9 +102,8 @@ public void invoke_rollsbackOnRequest() throws Throwable {
141102 // Expected.
142103 }
143104
144- verify (connection , statement , returnBinding );
105+ verify (connection ). rollback ( );
145106 }
146-
147107}
148108
149109interface Stubby {
0 commit comments