@@ -70,21 +70,20 @@ public void ruleIsIntroducedAndEvaluatedOnSubclass() {
70
70
71
71
private static int runCount ;
72
72
73
- public static class MultipleRuleTest {
74
- private static class Increment implements MethodRule {
75
- public Statement apply (final Statement base ,
76
- FrameworkMethod method , Object target ) {
77
- return new Statement () {
78
- @ Override
79
- public void evaluate () throws Throwable {
80
- runCount ++;
81
- base .evaluate ();
82
- }
83
-
84
- ;
85
- };
86
- }
73
+ private static class Increment implements MethodRule {
74
+ public Statement apply (final Statement base ,
75
+ FrameworkMethod method , Object target ) {
76
+ return new Statement () {
77
+ @ Override
78
+ public void evaluate () throws Throwable {
79
+ runCount ++;
80
+ base .evaluate ();
81
+ }
82
+ };
87
83
}
84
+ }
85
+
86
+ public static class MultipleRuleTest {
88
87
89
88
@ Rule
90
89
public MethodRule incrementor1 = new Increment ();
@@ -292,4 +291,122 @@ public void foo() {
292
291
public void useCustomMethodRule () {
293
292
assertThat (testResult (UsesCustomMethodRule .class ), isSuccessful ());
294
293
}
294
+
295
+ public static class HasMethodReturningMethodRule {
296
+ private MethodRule methodRule = new MethodRule () {
297
+
298
+ @ Override
299
+ public Statement apply (final Statement base , FrameworkMethod method , Object target ) {
300
+ return new Statement () {
301
+
302
+ @ Override
303
+ public void evaluate () throws Throwable {
304
+ wasRun = true ;
305
+ base .evaluate ();
306
+ }
307
+ };
308
+ }
309
+ };
310
+
311
+ @ Rule
312
+ public MethodRule methodRule () {
313
+ return methodRule ;
314
+ }
315
+
316
+ @ Test
317
+ public void doNothing () {
318
+
319
+ }
320
+ }
321
+
322
+ /**
323
+ * If there are any public methods annotated with @Rule returning a {@link MethodRule}
324
+ * then it should also be run.
325
+ *
326
+ * <p>This case has been added with
327
+ * <a href="https://github.com/junit-team/junit/issues/589">Issue #589</a> -
328
+ * Support @Rule for methods works only for TestRule but not for MethodRule
329
+ */
330
+ @ Test
331
+ public void runsMethodRuleThatIsReturnedByMethod () {
332
+ wasRun = false ;
333
+ JUnitCore .runClasses (HasMethodReturningMethodRule .class );
334
+ assertTrue (wasRun );
335
+ }
336
+
337
+ public static class HasMultipleMethodsReturningMethodRule {
338
+ @ Rule
339
+ public Increment methodRule1 () {
340
+ return new Increment ();
341
+ }
342
+
343
+ @ Rule
344
+ public Increment methodRule2 () {
345
+ return new Increment ();
346
+ }
347
+
348
+ @ Test
349
+ public void doNothing () {
350
+
351
+ }
352
+ }
353
+
354
+ /**
355
+ * If there are multiple public methods annotated with @Rule returning a {@link MethodRule}
356
+ * then all the rules returned should be run.
357
+ *
358
+ * <p>This case has been added with
359
+ * <a href="https://github.com/junit-team/junit/issues/589">Issue #589</a> -
360
+ * Support @Rule for methods works only for TestRule but not for MethodRule
361
+ */
362
+ @ Test
363
+ public void runsAllMethodRulesThatAreReturnedByMethods () {
364
+ runCount = 0 ;
365
+ JUnitCore .runClasses (HasMultipleMethodsReturningMethodRule .class );
366
+ assertEquals (2 , runCount );
367
+ }
368
+
369
+
370
+ public static class CallsMethodReturningRuleOnlyOnce {
371
+ int callCount = 0 ;
372
+
373
+ private static class Dummy implements MethodRule {
374
+
375
+ @ Override
376
+ public Statement apply (final Statement base , FrameworkMethod method , Object target ) {
377
+ return new Statement () {
378
+
379
+ @ Override
380
+ public void evaluate () throws Throwable {
381
+ base .evaluate ();
382
+ }
383
+ };
384
+ }
385
+ };
386
+
387
+
388
+ @ Rule
389
+ public MethodRule methodRule () {
390
+ callCount ++;
391
+ return new Dummy ();
392
+ }
393
+
394
+ @ Test
395
+ public void doNothing () {
396
+ assertEquals (1 , callCount );
397
+ }
398
+ }
399
+
400
+ /**
401
+ * If there are any public methods annotated with @Rule returning a {@link MethodRule}
402
+ * then method should be called only once.
403
+ *
404
+ * <p>This case has been added with
405
+ * <a href="https://github.com/junit-team/junit/issues/589">Issue #589</a> -
406
+ * Support @Rule for methods works only for TestRule but not for MethodRule
407
+ */
408
+ @ Test
409
+ public void callsMethodReturningRuleOnlyOnce () {
410
+ assertTrue (JUnitCore .runClasses (CallsMethodReturningRuleOnlyOnce .class ).wasSuccessful ());
411
+ }
295
412
}
0 commit comments