junit的Rule使用方法介绍

简介

通过java annotations(注解)形式将TestRule或TestRule子类的实例添加到测试类中,来扩展各种测试功能。

环境

  • java 1.8
  • gradle

文件目录

.
├── main
│   ├── java
│   └── resources
└── test
    ├── java
    │   └── example
    │       ├── TestTemporaryFolder.java
    │       └── TestTestName.java
    └── resources

build.gradle
group 'example'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile 'junit:junit:4.12'
    compile 'junit:junit:4.12'
}

test {
    testLogging.showStandardStreams = true
}
Rule – TestName
package example;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.Description;

import static org.junit.Assert.assertEquals;

public class TestTestName
{
    @Rule
    public TestName testName = new TestName()
    {
        @Override
        public void starting(Description d)
        {
            System.out.println(
                    String.format("%s starting ...", d.getMethodName()));
            super.starting(d);
        }
    };

    @Test
    public void testMethodA()
    {
        System.out.println(
                String.format("%s running ...", testName.getMethodName()));
        assertEquals("testMethodA", testName.getMethodName());
    }

    @Test
    public void testMethodB()
    {
        System.out.println(
                String.format("%s running ...", testName.getMethodName()));
        assertEquals("testMethodB", testName.getMethodName());
    }
}
$ gradle -Dtest.single=TestTestName test

> Task :test 

example.TestTestName > testMethodA STANDARD_OUT
    testMethodA starting ...
    testMethodA running ...

example.TestTestName > testMethodB STANDARD_OUT
    testMethodB starting ...
    testMethodB running ...


BUILD SUCCESSFUL in 2s
2 actionable tasks: 1 executed, 1 up-to-date

http://junit.org/junit4/javadoc/4.12/org/junit/rules/TestName.html

Rule – TemporaryFolder
package example;

import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.junit.Assert.assertTrue;

public class TestTemporaryFolder
{
    File tempFile = null;

    @Rule
    public TemporaryFolder tempFolder = new TemporaryFolder()
    {
        @Override
        protected void before() throws Throwable
        {
            System.out.println("TemporaryFolder.before running ...");
            super.before();
        }

        @Override
        protected void after()
        {
            System.out.println("TemporaryFolder.after running ...");
            super.delete();
        }
    };

    @Before
    public void before() throws IOException
    {
        System.out.println("TestTemporaryFolder.before running ...");
        tempFile = tempFolder.newFile();
    }

    @After
    public void after()
    {
        System.out.println("TestTemporaryFolder.after running ...");
    }

    @Test
    public void testMethodA()
    {
        System.out.println("testMethodA running ...");
        System.out.println(String.format("tempFile name: %s", tempFile.getName()));
        assertTrue(tempFile.exists());
    }

    @Test
    public void testMethodB()
    {
        System.out.println("testMethodA running ...");
        System.out.println(String.format("tempFile name: %s", tempFile.getName()));
        assertTrue(tempFile.exists());
    }
}
$ gradle -Dtest.single=TestTemporaryFolder test

> Task :test 

example.TestTemporaryFolder > testMethodA STANDARD_OUT
    TemporaryFolder.before running ...
    TestTemporaryFolder.before running ...
    testMethodA running ...
    tempFile name: junit6896407163717284433.tmp
    TestTemporaryFolder.after running ...
    TemporaryFolder.after running ...

example.TestTemporaryFolder > testMethodB STANDARD_OUT
    TemporaryFolder.before running ...
    TestTemporaryFolder.before running ...
    testMethodA running ...
    tempFile name: junit6538524456215426695.tmp
    TestTemporaryFolder.after running ...
    TemporaryFolder.after running ...


BUILD SUCCESSFUL in 2s
2 actionable tasks: 1 executed, 1 up-to-date

http://junit.org/junit4/javadoc/4.12/org/junit/rules/TemporaryFolder.html

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注