JUnit annotation of Rule

 

  Bloggers recommended articles: http://www.testclass.net/junit

 

A JUnit Rule is a class that implements TestRule, similar to the role of these classes  @Before, @Afterit is a method for performing some code before and after the execution of each test method. So why does not directly use these  @Before, @Aftertoo? This is because they can only act on a class, if you need to use the same setup at the same time in two classes there, then you have to test in two classes defined inside the same @Before method, which then write the same code, which and causing code duplication.

In addition, JUnit Rule can do something  @Before, @Afterthese notes can not do the things that they can dynamically obtain information about the test class to run, test methods.

Use frame comes with Rule

In addition to increasing Rule feature, the new version of JUnit also added a lot of core Rule

  • TemporaryFolder: test files and directories can be created and will be deleted after the end of the test run. This is useful for those tests dealing with the file system and run independently of it.
  • ExternalResource: This is a resource usage pattern, it will create a good resource in advance and will be destroyed after the end of the test. This is useful for those who use the test socket, and other resources for the embedded server.
  • ErrorCollector: allows tests continue to run and reports any errors at the end of the test after a failure. This is for those who need to verify a number of independent test conditions for useful (although this in itself may be a "test smell"). 
  • ExpectedException: exception message may specify a desired type and in the test.
  • Timeout: for all test applications in the same category timeout.

For example, TimeOut use of this Rule of.

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;


public class RuleTestDemo {

    //使用Timeout这个Rule
    @Rule
    public Timeout timeout = new Timeout(1000);  

    @Test
    public void testMethod1() throws Exception {
        Thread.sleep(1001);
    }

    @Test
    public void testMethod2() throws Exception {
        Thread.sleep(999);
    }
}

 

Timeout Use provided JUnit class, the class for controlling the execution of test cases timeout. Here set to 1 second, when the execution of the failure cases more than one second. Next testMethod1 were used in two use cases and testMethod2 sleep () method to control execution time of use cases, apparently testMethod1 than 1 second, the operation fails.

 

Custom Rule

In addition to using the JUnit framework comes with the Rule, Rule also can customize according to their needs. In simple terms, the custom is to implement a Rule a TestRule interface and implement apply () method. This method needs to return a Statement object. Examples are as follows:

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;


public class MethodNameRule implements TestRule {

    public Statement apply(final Statement base, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                //在测试方法运行之前做一些事情,在base.evaluate()之前
                String className = description.getClassName();
                MethodName String =description.getMethodName (); 

                base.evaluate ();   // run the test method 

                // do something after the test method runs in base.evaluate () after 
                System.out.println ( "Class name:" + className + ", Method name:" + methodName); 
            } 
        }; 
    } 
}

 

Here is the function to run after each test case, print the current test case class and method names. Add MethodNameRule defined herein in the example above.

......
 public  class RuleTestDemo { 

    // use this Timeout the Rule 
    @rule
     public Timeout timeout = new new Timeout (1000 );   

    // Use Custom the Rule, 
    @rule
     public MethodNameRule methodNameRule = new new MethodNameRule (); 

......

 

Re-run the test case execution results are as follows:

 

 

 

 

Article reprint to: http://www.testclass.net/junit/rule

Guess you like

Origin www.cnblogs.com/nhdlb/p/11245829.html