2020import static org .junit .Assert .assertEquals ;
2121import static org .junit .Assert .assertNotNull ;
2222import static org .junit .Assert .assertTrue ;
23+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2324
2425import java .util .Arrays ;
2526
3132
3233public abstract class AbstractStubTestCase extends AbstractProxyFactoryAgnosticTest
3334{
35+
3436 // *****************************************************************************************************************
3537 // Fields
3638 // *****************************************************************************************************************
@@ -68,18 +70,17 @@ protected void train(StubInterface trainee)
6870 assertEquals ("World" , proxy .one (null ));
6971 }
7072
71- @ Test ( expected = IllegalStateException . class )
73+ @ Test
7274 public void testMixingArgumentMatchingStrategies ()
7375 {
74- createProxy (new Trainer <StubInterface >()
75- {
76- @ Override
77- protected void train (StubInterface trainee )
78- {
79- when (trainee .three (isInstance (String .class ), "World" ))
80- .thenAnswer (ObjectProviderUtils .constant ("World" ));
81- }
82- });
76+ assertThrows (IllegalStateException .class , () ->
77+ createProxy (new Trainer <StubInterface >() {
78+ @ Override
79+ protected void train (StubInterface trainee ) {
80+ when (trainee .three (isInstance (String .class ), "World" ))
81+ .thenAnswer (ObjectProviderUtils .constant ("World" ));
82+ }
83+ }));
8384 }
8485
8586 @ Test
@@ -134,23 +135,22 @@ protected void train(StubInterface trainee)
134135 assertEquals ("One" , proxy .stubs ()[1 ].one ("Whatever" ));
135136 }
136137
137- @ Test ( expected = IllegalStateException . class )
138+ @ Test
138139 public void testThenBeforeWhen ()
139140 {
140- createProxy (new Trainer <StubInterface >()
141- {
142- @ Override
143- protected void train (StubInterface trainee )
144- {
145- thenThrow (new RuntimeException ("Oops!" ));
146- }
147- });
141+ assertThrows (IllegalStateException .class , () ->
142+ createProxy (new Trainer <StubInterface >() {
143+ @ Override
144+ protected void train (StubInterface trainee ) {
145+ thenThrow (new RuntimeException ("Oops!" ));
146+ }
147+ }));
148148 }
149149
150- @ Test ( expected = IllegalArgumentException . class )
150+ @ Test
151151 public void testThrowExceptionWithException ()
152152 {
153- StubInterface proxy = createProxy (new Trainer <StubInterface >()
153+ final StubInterface proxy = createProxy (new Trainer <StubInterface >()
154154 {
155155 @ Override
156156 protected void train (StubInterface trainee )
@@ -159,13 +159,13 @@ protected void train(StubInterface trainee)
159159 thenThrow (new IllegalArgumentException ("Nope!" ));
160160 }
161161 });
162- proxy .voidMethod ("Hello" );
162+ assertThrows ( IllegalArgumentException . class , () -> proxy .voidMethod ("Hello" ) );
163163 }
164164
165- @ Test ( expected = IllegalArgumentException . class )
165+ @ Test
166166 public void testThrowExceptionWithProvidedException ()
167167 {
168- StubInterface proxy = createProxy (new Trainer <StubInterface >()
168+ final StubInterface proxy = createProxy (new Trainer <StubInterface >()
169169 {
170170 @ Override
171171 protected void train (StubInterface trainee )
@@ -174,10 +174,10 @@ protected void train(StubInterface trainee)
174174 thenThrow (ObjectProviderUtils .constant (new IllegalArgumentException ("Nope!" )));
175175 }
176176 });
177- proxy .voidMethod ("Hello" );
177+ assertThrows ( IllegalArgumentException . class , () -> proxy .voidMethod ("Hello" ) );
178178 }
179179
180- @ Test ( expected = RuntimeException . class )
180+ @ Test
181181 public void testThrowingExceptionObject ()
182182 {
183183 final StubInterface proxy = createProxy (new Trainer <StubInterface >()
@@ -188,10 +188,10 @@ protected void train(StubInterface trainee)
188188 when (trainee .one ("Hello" )).thenThrow (new RuntimeException ("No way, Jose!" ));
189189 }
190190 });
191- proxy .one ("Hello" );
191+ assertThrows ( RuntimeException . class , () -> proxy .one ("Hello" ) );
192192 }
193193
194- @ Test ( expected = RuntimeException . class )
194+ @ Test
195195 public void testThrowingProvidedException ()
196196 {
197197 final StubInterface proxy = createProxy (new Trainer <StubInterface >()
@@ -203,27 +203,24 @@ protected void train(StubInterface trainee)
203203 ObjectProviderUtils .constant (new RuntimeException ("No way, Jose!" )));
204204 }
205205 });
206- proxy .one ("Hello" );
206+ assertThrows ( RuntimeException . class , () -> proxy .one ("Hello" ) );
207207 }
208208
209- @ Test ( expected = IllegalStateException . class )
209+ @ Test
210210 public void testUsingWrongStub ()
211211 {
212- createProxy (new Trainer <StubInterface >()
213- {
214- @ Override
215- protected void train (final StubInterface parent )
216- {
217- when (parent .stub ()).thenStub (new Trainer <StubInterface >()
218- {
212+ assertThrows (IllegalStateException .class , () ->
213+ createProxy (new Trainer <StubInterface >() {
219214 @ Override
220- protected void train (final StubInterface child )
221- {
222- when (parent .one ("Hello" )).thenReturn ("World" );
215+ protected void train (final StubInterface parent ) {
216+ when (parent .stub ()).thenStub (new Trainer <StubInterface >() {
217+ @ Override
218+ protected void train (final StubInterface child ) {
219+ when (parent .one ("Hello" )).thenReturn ("World" );
220+ }
221+ });
223222 }
224- });
225- }
226- });
223+ }));
227224 }
228225
229226 @ Test
@@ -453,4 +450,5 @@ protected void train(StubInterface trainee)
453450 assertNotNull (proxy .stub ());
454451 assertEquals ("World" , proxy .stub ().one ("Hello" ));
455452 }
453+
456454}
0 commit comments