org.junit.rules.MethodRule and org.junit.rules.TestWatchman have been deprecated.
one interesting note was at: https://github.com/junit-team/junit/pull/519 , in part:
“Many developers are legitimate reasons to stick with MethodRule and the JUnit team has no plans to remove support for MethodRule…”
http://junit-team.github.io/junit/javadoc/4.10/org/junit/rules/TestWatchman.html documents:
“Deprecated. MethodRule is deprecated. Use TestWatcher implements TestRule instead.” and provides some example code.
What is the reasoning behind marking these deprecated?
What is the tradeoff between TestWatcher and the deprecated TestWachman?
Do you have a good link for a synopsis or overview on this specific topic?
The reason is simple,
TestRulewas planned to replaceMethodRule.MethodRulewas introduced implemented in 4.7, and it is an interface with one method:FrameworkMethodis (almost) an internal JUnit class, which shouldn’t have been exposed in the first place.objectis the object on which the method will be run, so for example, you can modify state of the test using reflection.TestRulewas introduced in 4.9, however, is:Descriptionis a immutable POJO containing the description of the test. The way to modify state within a test is to encapsulate correctly within the test using aTestRule. It’s an altogether cleaner design.The specific difference between
TestWatchman(MethodRule)andTestWatcher(TestRule)is minimal, except that TestWatcher has better error handling, so this should be used in preference. Both have overridable methods such assucceeded(),failed(),starting(),finished().TestWatcher(TestRule)handles exceptions in the overidden methods. If exceptions are thrown, then the test method fails after execution of the test, rather than during.For more information, see TestWatcher and TestWatchman