In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to use Rule. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Discuss a way to extend JUnit4, that is, directly modify the implementation of Test Runner (BlockJUnit4ClassRunner). However, this approach is obviously not convenient to flexibly add or remove extended functions. Here we will use Rule, the extension method that JUnit4.7 has just begun to introduce, to achieve the same extension functionality.
1. Rule
Rule is an extension that JUnit4.7 has just begun to provide, and it can replace most of the existing Runner extensions. JUnit contains two types of Rule Annotation:@ClassRule and @ Rule. @ ClassRule applies to static variables in the test class, while @ Rule applies to member variables; similarly, these variables must be instances of the TestRule interface and the access modifier must be public.
BlockJUnit4ClassRunner has been extended, and the extended method is methodBlock. Now let's take a look at the code in the body of the method:
Protected Statement methodBlock (FrameworkMethod method) {Object test; try {test= new ReflectiveCallable () {@ Override protected Object runReflectiveCall () throws Throwable {return createTest ();}} .run ();} catch (Throwable e) {return new Fail (e);} Statement statement= methodInvoker (method, test); statement= possiblyExpectingExceptions (method, test, statement); statement= withPotentialTimeout (method, test, statement); statement= withBefores (method, test, statement); statement= withAfters (method, test, statement) Statement= withRules (method, test, statement); return statement;}
But in BlockJUnit4ClassRunner, possiblyExpectingExceptions (), withPotentialTimeout (), withBefores (), and withAfters () have been marked as obsolete, and JUnit recommends using Rule to replace the functionality of these methods.
2. TestLogRule
As described in section 1, if Rule Annotation is to act on an instance of the TestRule interface, you must first create an implementation class for TestRule.
Public class TestLogRule implements TestRule {private static final DateFormat format = new SimpleDateFormat ("yyyy-MM-dd_HH:mm:ss_SSS"); @ Override public Statement apply (Statement base, Description description) {TestLogger testLogger = description.getAnnotation (TestLogger.class); if (testLogger! = null) {StringBuilder log = new StringBuilder (format.format (new Date () Log.append ("") .append (description.getClassName ()) .append ("#") .append (description.getMethodName ()) .append (":") .append (testLogger.log ()); System.out.println (log.toString ());} return base;}}
As shown above, TestLogRule has a lot in common with the LoggedRunner code in the previous blog post, with the function of printing out the specified log, and each line of log is prefixed with the current execution time and the full method name.
3. CalculatorTest using Rule
Here is the new test class CalculatorTest, which will not use BlockJUnit4ClassRunner's extension LoggedRunner as the test executor, so this class does not use @ RunWith (LoggedRunner.class), so BlockJUnit4ClassRunner will still be used when executing the test class.
Public class CalculatorTest {private static Calculator calculator = null; @ Rule public TestLogRule testLogRule = new TestLogRule (); @ BeforeClass public static void createCalculator () {calculator = new Calculator ();} @ Test @ TestLogger (log = "a simple division") public void simpleDivide () {int value = calculator.divide (8,2); Assert.assertTrue (value = = 4);} @ Test (expected = ArithmeticException.class) @ TestLogger (log = "divided by zero, and an ArithmeticException thrown.") Public void dividedByZero () {calculator.divide (8,0);}}
Compared with the CalculatorTest in the previous blog post, the CalculatorTest in this article has two more lines of code in addition to not using LoggedRunner:
@ Rule public TestLogRule testLogRule = new TestLogRule ()
Before executing the unit test method, BlockJUnit4ClassRunner calls the apply () method in TestRule/TestLogRule, that is, the log contents are printed out first.
4. Summary
Using Rule to extend JUnit avoids the extension of the default Runner, it is convenient to add or remove Rule for the test class, and the Rule implementation class itself can be easily reused.
The above is the editor for you to share how to use Rule, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.