TestRule ou une instance d'une classe qui hérite de TestRule En ajoutant l'annotation @Rule et en la définissant dans le champ de la classe de test Il est possible d'ajouter diverses fonctions au test.
http://junit.org/junit4/javadoc/4.12/org/junit/rules/TestRule.html
$ 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