如何配置JUnit规则以应用于特定测试

时间:2012-04-22 07:14:35

标签: java junit

是否可以将Junit规则仅适用于特定测试?如果是这样,我该怎么做?

下面的代码举例说明了我想要做的事情:每次我有@Rule时,我都希望下面的方法能够使用已经注释的特定规则来运行它。我只希望该规则与相应的测试一起运行。我不希望任何其他测试受到规则的影响。

在这种情况下,当我运行这些测试时,我看到其中一个测试是EmptyFileCheck,给出了一个File DNE不存在,但是我为该函数使用了一个单独的注释,所以我原以为它会运行使用不同的上下文,提供Empty,但DNE直到被使用。

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);
    }
}

2 个答案:

答案 0 :(得分:2)

你的规则中可以有一个setter,这是规则中第一个被调用的东西。这样的事情来自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?");
     }
}

答案 1 :(得分:2)

有什么理由不将代码从@rule注释中取出并将其移到测试体的开头?