Notes on how to use each JUnit Rule

What is Rule

TestRule or an instance of a class that inherits TestRule By annotating @Rule and setting it in the field of the test class It is possible to add various functions to the test.

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

Constitution

$ tree
.
├── build.gradle
└── src
    └── test
        └── java
            └── example
                ├── TestTemporaryFolder.java
                └── TestTestName.java
$ cat build.gradle
apply plugin: "java"

repositories {
  mavenCentral()
}

dependencies {
  testCompile "junit:junit:4.+"
}

test {
  testLogging.showStandardStreams = true
}

TestName

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

TestTestName.java


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 1s
2 actionable tasks: 1 executed, 1 up-to-date

TemporaryFolder

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

TestTemporaryFolder.java


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: junit2803814005363359855.tmp
    TestTemporaryFolder.after running ...
    TemporaryFolder.after running ...

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


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

Recommended Posts

Notes on how to use each JUnit Rule
How to use JUnit 5
How to use JUnit (beginner)
[Creating] How to use JUnit
Notes on how to use regular expressions in Java
How to use Ruby on Rails
How to use Bio-Formats on Ubuntu 20.04
How to use Apache Derby on Eclipse
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
[Ruby on Rails] How to use session method
Notes on how to create Burp Suite extensions
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
Notes on how to write comments in English
How to use Map
How to use java non-standard library on IntelliJ IDEA
[Java] How to use Map
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use Ruby return
How to deploy on heroku
[Rails] How to use enum
[Swift] How to use UserDefaults
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to write Junit 5 organized
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!