11package org .baeldung .mocks .jmockit ;
22
3- import static org .junit .Assert .*;
4-
5- import java .util .ArrayList ;
6- import java .util .List ;
7-
8- import org .hamcrest .BaseMatcher ;
9- import org .hamcrest .Description ;
10- import org .junit .Test ;
11- import org .junit .runner .RunWith ;
12-
133import mockit .Delegate ;
144import mockit .Expectations ;
155import mockit .Mocked ;
166import mockit .StrictExpectations ;
177import mockit .Verifications ;
188import mockit .integration .junit4 .JMockit ;
9+ import org .hamcrest .BaseMatcher ;
10+ import org .hamcrest .Description ;
11+ import org .junit .Test ;
12+ import org .junit .runner .RunWith ;
13+
14+ import java .util .ArrayList ;
15+ import java .util .List ;
16+
17+ import static org .junit .Assert .assertEquals ;
1918
2019@ RunWith (JMockit .class )
2120@ SuppressWarnings ("unchecked" )
2221public class ExpectationsTest {
2322
2423 @ Test
2524 public void testForAny (@ Mocked ExpectationsCollaborator mock ) throws Exception {
26- new Expectations () {
27- {
28- mock .methodForAny1 (anyString , anyInt , anyBoolean );
29- result = "any" ;
30- }
31- };
25+ new Expectations () {{
26+ mock .methodForAny1 (anyString , anyInt , anyBoolean );
27+ result = "any" ;
28+ }};
3229
3330 assertEquals ("any" , mock .methodForAny1 ("barfooxyz" , 0 , Boolean .FALSE ));
3431 mock .methodForAny2 (2L , new ArrayList <>());
3532
36- new Verifications () {
37- {
38- mock .methodForAny2 (anyLong , (List <String >) any );
39- }
40- };
33+ new Verifications () {{
34+ mock .methodForAny2 (anyLong , (List <String >) any );
35+ }};
4136 }
4237
4338 @ Test
4439 public void testForWith (@ Mocked ExpectationsCollaborator mock ) throws Exception {
45- new Expectations () {
46- {
47- mock .methodForWith1 (withSubstring ("foo" ), withNotEqual (1 ));
48- result = "with" ;
49- }
50- };
51-
40+ new Expectations () {{
41+ mock .methodForWith1 (withSubstring ("foo" ), withNotEqual (1 ));
42+ result = "with" ;
43+ }};
44+
5245 assertEquals ("with" , mock .methodForWith1 ("barfooxyz" , 2 ));
5346 mock .methodForWith2 (Boolean .TRUE , new ArrayList <>());
54-
55- new Verifications () {
56- {
57- mock .methodForWith2 (withNotNull (), withInstanceOf (List .class ));
58- }
59- };
47+
48+ new Verifications () {{
49+ mock .methodForWith2 (withNotNull (), withInstanceOf (List .class ));
50+ }};
6051 }
6152
6253 @ Test
6354 public void testWithNulls (@ Mocked ExpectationsCollaborator mock ) {
64- new Expectations () {
65- {
66- mock .methodForNulls1 (anyString , null );
67- result = "null" ;
68- }
69- };
70-
55+ new Expectations () {{
56+ mock .methodForNulls1 (anyString , null );
57+ result = "null" ;
58+ }};
59+
7160 assertEquals ("null" , mock .methodForNulls1 ("blablabla" , new ArrayList <String >()));
7261 mock .methodForNulls2 ("blablabla" , null );
73-
74- new Verifications () {
75- {
76- mock .methodForNulls2 (anyString , (List <String >) withNull ());
77- }
78- };
62+
63+ new Verifications () {{
64+ mock .methodForNulls2 (anyString , (List <String >) withNull ());
65+ }};
7966 }
8067
8168 @ Test
8269 public void testWithTimes (@ Mocked ExpectationsCollaborator mock ) {
83- new Expectations () {
84- {
85- mock .methodForTimes1 (); times = 2 ;
86- mock .methodForTimes2 ();
87- }
88- };
89-
70+ new Expectations () {{
71+ mock .methodForTimes1 ();
72+ times = 2 ;
73+ mock .methodForTimes2 ();
74+ }};
75+
9076 mock .methodForTimes1 ();
9177 mock .methodForTimes1 ();
9278 mock .methodForTimes2 ();
9379 mock .methodForTimes3 ();
9480 mock .methodForTimes3 ();
9581 mock .methodForTimes3 ();
96-
97- new Verifications () {
98- {
99- mock . methodForTimes3 (); minTimes = 1 ; maxTimes = 3 ;
100- }
101- };
82+
83+ new Verifications () {{
84+ mock . methodForTimes3 ();
85+ minTimes = 1 ;
86+ maxTimes = 3 ;
87+ }} ;
10288 }
10389
10490 @ Test
10591 public void testCustomArgumentMatching (@ Mocked ExpectationsCollaborator mock ) {
106- new Expectations () {
107- {
108- mock .methodForArgThat (withArgThat (new BaseMatcher <Object >() {
109- @ Override
110- public boolean matches (Object item ) {
111- return item instanceof Model && "info" .equals (((Model ) item ).getInfo ());
112- }
92+ new Expectations () {{
93+ mock .methodForArgThat (withArgThat (new BaseMatcher <Object >() {
94+ @ Override
95+ public boolean matches (Object item ) {
96+ return item instanceof Model && "info" .equals (((Model ) item ).getInfo ());
97+ }
11398
114- @ Override
115- public void describeTo (Description description ) { }
116- }));
117- }
118- };
99+ @ Override
100+ public void describeTo (Description description ) {
101+ }
102+ }));
103+ }} ;
119104 mock .methodForArgThat (new Model ());
120105 }
121106
122107 @ Test
123108 public void testResultAndReturns (@ Mocked ExpectationsCollaborator mock ) {
124- new StrictExpectations () {
125- {
126- mock .methodReturnsString ();
127- result = "foo" ;
128- result = new Exception ();
129- result = "bar" ;
130- mock .methodReturnsInt ();
131- result = new int [] { 1 , 2 , 3 };
132- mock .methodReturnsString ();
133- returns ("foo" , "bar" );
134- mock .methodReturnsInt ();
135- result = 1 ;
136- }
137- };
138-
109+ new StrictExpectations () {{
110+ mock .methodReturnsString ();
111+ result = "foo" ;
112+ result = new Exception ();
113+ result = "bar" ;
114+ mock .methodReturnsInt ();
115+ result = new int []{1 , 2 , 3 };
116+ mock .methodReturnsString ();
117+ returns ("foo" , "bar" );
118+ mock .methodReturnsInt ();
119+ result = 1 ;
120+ }};
121+
139122 assertEquals ("Should return foo" , "foo" , mock .methodReturnsString ());
140123 try {
141124 mock .methodReturnsString ();
@@ -153,24 +136,23 @@ public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
153136
154137 @ Test
155138 public void testDelegate (@ Mocked ExpectationsCollaborator mock ) {
156- new Expectations () {
157- {
158- mock .methodForDelegate (anyInt );
159- result = new Delegate () {
160- public int delegate (int i ) throws Exception {
161- if (i < 3 ) {
162- return 5 ;
163- } else {
164- throw new Exception ();
165- }
139+ new Expectations () {{
140+ mock .methodForDelegate (anyInt );
141+ result = new Delegate () {
142+ public int delegate (int i ) throws Exception {
143+ if (i < 3 ) {
144+ return 5 ;
145+ } else {
146+ throw new Exception ();
166147 }
167- };
168- }
169- };
170-
148+ }
149+ };
150+ }} ;
151+
171152 assertEquals ("Should return 5" , 5 , mock .methodForDelegate (1 ));
172153 try {
173154 mock .methodForDelegate (3 );
174- } catch (Exception e ) { }
155+ } catch (Exception e ) {
156+ }
175157 }
176158}
0 commit comments