In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to expand Runner in JUnit4, aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
In the process of using JUnit, you may make some extensions to JUnit. The example in this article defines a new Annotation for JUnit4 and extends the existing Runner accordingly to parse the newly introduced Annotation.
Invent an example that automatically prints a log for a unit test method before executing it. This example defines a new Annotation for JUnit to specify the log content to be printed, and extends the Runner implementation BlockJUnit4ClassRunner provided by JUnit by default to recognize the new Annotation.
1. Define Annotation
TestLogger is an Annotation that acts on a method. It has only one property that specifies the contents of the log. The code is as follows
@ Target ({ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) public @ interface TestLogger {public String log () default "";}
two。 Extended Runner
JUnit provides several implementations of Runner, such as BlockJUnit4ClassRunner,Suite, where BlockJUnit4ClassRunner is used to execute a single test case class. LoggedRunner will extend BlockJUnit4ClassRunner to override the methodBlock () method in it. The new methodBlock () method initially attempts to get the TestLogger Annotation in the test method being executed, and if it exists, prints out the specified log, each line prefixed with the current execution time and the full method name. The code for this class is as follows
Public class LoggedRunner extends BlockJUnit4ClassRunner {private static final DateFormat format = new SimpleDateFormat ("yyyy-MM-dd_HH:mm:ss_SSS"); public LoggedRunner (Class klass) throws InitializationError {super (klass);} @ Override protected Statement methodBlock (FrameworkMethod method) {Method classMethod = method.getMethod (); TestLogger loggerAnnotation = classMethod.getAnnotation (TestLogger.class); if (loggerAnnotation! = null) {StringBuilder log = new StringBuilder (format.format (new Date ()) Log.append ("") .append (classMethod.getDeclaringClass (). GetName ()) .append ("#") .append (classMethod.getName ()) .append (":") .append (loggerAnnotation.log ()); System.out.println (log.toString ());} return super.methodBlock (method);}}
3. Application program
Calculator is a simple application in which a division method is defined, with the following code
Public class Calculator {public int divide (int a, int b) {return a / b;}}
4. Unit test program
CalculatorTest is a simple unit test program that unit tests the divide () method in Calculator in two ways. The code is as follows
RunWith (LoggedRunner.class) public class CalculatorTest {private static Calculator calculator = null; @ 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);}}
It is worth noting that CalculatorTest specifically specifies LoggedRunner as the test executor (@ RunWith (LoggedRunner.class)); at the same time, each unit test method, simpleDivide () and dividedByZero (), uses Annotation TestLogger to specify the log contents. When the above unit test is executed, the log contents in the following form are automatically printed:
2011-12-13-23 test.CalculatorTest#dividedByZero 48 test.CalculatorTest#dividedByZero 38-218 test.CalculatorTest#simpleDivide: a simple division2011-12-13-23 test.CalculatorTest#dividedByZero: divided by zero, and an ArithmeticException thrown.
5. Summary
By extending BlockJUnit4ClassRunner, you can make JUnit do some extra work when running test cases. However, this direct modification of the default Test Runner is not advocated, and the use of Test Rule for the same extension will be introduced in the next article.
This is the answer to the question about how to expand Runner in JUnit4. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.