How to configure JUnit rules to apply to specific test
java, junit
Solution
You could have a setter in your Rule which is the first thing that gets called in the rule. Something like this, from ExpectedException:
// These tests all pass.
public static class HasExpectedException {
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test
public void throwsNothing() {
// no exception expected, none thrown: passes.
}
@Test
public void throwsNullPointerException() {
thrown.expect(NullPointerException.class);
throw new NullPointerException();
}
@Test
public void throwsNullPointerExceptionWithMessage() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("happened?");
thrown.expectMessage(startsWith("What"));
throw new NullPointerException("What happened?");
}
}
Problem
Is it possible to have a Junit rule only apply to specific tests? If so, how do I do that? The code below exemplifies what I want to do: each time I have @Rule, I want the method below that to have the specific rule that has been annotated to run with it. I only want that rule to run with the corresponding test. I don't want anything other tests to be affected by the rule. In this case, when I run these tests, I see that one of the tests the EmptyFileCheck, gives a File DNE does not exist, but I have used a separate annotation for that function, so I had thought that it would run with a different context, supplying the Empty, but instead DNE is till being used. ``` import static java.lang.System.in; import static java.lang.System.setIn; import static org.junit.Assert.fail; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.nio.channels.Pipe; import static org.hamcrest.core.AllOf.allOf; import org.hamcrest.Matcher; import org.hamcrest.core.AllOf; import static org.hamcrest.Matchers.*; import org.hamcrest.text.StringContains; import org.hamcrest.text.StringEndsWith; import org.hamcrest.text.StringStartsWith; import org.jmock.Expectations; import org.jmock.Mockery; import org.jmock.lib.legacy.ClassImposteriser; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.TextFromStandardInputStream; import org.junit.runner.RunWith; public class UnitTests { private Mockery context = new Mockery() {{ setImposteriser(ClassImposteriser.INSTANCE); }}; private main mn; private InputStream oldIn; private PrintStream oldOut; private InputStream mockIn; private InputStreamReader mockinputStream; private PrintStream mockOut; private BufferedReader reader; private Expectations exp; @Before public void setMinimalMockingExpectations() throws IOException { exp = new Expectations() {{ }}; mn = context.mock(main.class); mockinputStream = context.mock(InputStreamReader.class); oldIn = System.in; oldOut = System.out; mockIn = context.mock(InputStream.class); mockOut = context.mock(PrintStream.class); System.setOut(mockOut); } public void configureExpectations(boolean fileOrInput, boolean verbosity) { exp.one(mockOut).println("Do you want to process standard (I)nput, or a (F)ile? I/F"); if (fileOrInput) { //it's a file exp.one(mockOut).println("Enter filename: "); } else { //it's not } } @After public void reset() { System.setOut(oldOut); } @Rule public final TextFromStandardInputStream FileNotFoundException = new TextFromStandardInputStream("F\nDNE\n"); @Test(expected=FileNotFoundException.class) public void EnsureFileCheckExists() throws IOException { final String fileName = "DNE"; configureExpectations(true, false); exp.one(mn).checkFile(fileName); context.checking(exp); mn.main(null); } @Rule public final TextFromStandardInputStream FileReadAccessDenied = new TextFromStandardInputStream("F\nUnderPriviledged\n");:w @Test(expected=FileNotFoundException.class) public void FileReadAccessDenied() throws java.io.FileNotFoundException { final String fileName = "UnderPriviledged"; configureExpectations(true, false); //exp.oneOf(mn).checkFile(with()); TODO: fix ME! context.checking(exp); mn.main(null); } @Rule public final TextFromStandardInputStream EmptyFileCheck = new TextFromStandardInputStream("F\nEmpty\n"); @Test public void EmptyFileCheck() throws java.io.FileNotFoundException { final String fileName = "Empty"; configureExpectations(true, false); exp.one(mn).checkFile(fileName); context.checking(exp); mn.main(null); } } ```