Java 使用Junit测试InputStream

Java 使用Junit测试InputStream,java,junit,inputstream,Java,Junit,Inputstream,我正在尝试测试类“UserInput”中的函数“DataFeatures” 不管我在测试中给出什么样的论点,它总是通过的 字段和构造函数 public @Getter class UserInput { FileType type; FileOperation operation; SynchronizationMethod method; String path; private static InputReader in = new InputRead

我正在尝试测试类“UserInput”中的函数“DataFeatures”

不管我在测试中给出什么样的论点,它总是通过的

字段和构造函数

public @Getter class UserInput {
    FileType type;
    FileOperation operation;
    SynchronizationMethod method;
    String path;
    private static InputReader in = new InputReader();

    public UserInput() {
        // Dont need to do anything
    }
要测试的功能

    void getDataFeatures() {
    System.out.println("For encryption press 1");
    System.out.println("For decryption press 0");
    operation = FileOperation.fromInt(in.nextInt());

    System.out.println("For a file choose 1");
    System.out.println("For an entire directory choose 0");
    type = FileType.fromInt(in.nextInt());

    if (type == FileType.DIR) {
        System.out.println("For sync press 1");
        System.out.println("For async press 0");
        method = SynchronizationMethod.fromInt(in.nextInt());
    }
}
我的测试

public class UserInputTest {

UserInput UI;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream oldStdOut = System.out;
private final PrintStream oldStdErr = System.err;
private final InputStream oldStdIn = System.in;

@Before
public void initlize(){
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));
    UI = new UserInput();
}

@Test
public void getDataFeaturesTest() {
    String data = "0" + "\n0" + "\n0";
    System.setIn(new ByteArrayInputStream(data.getBytes()));
    UI.getDataFeatures();
    System.out.println(UI.getOperation());
    assertThat(UI.getOperation(), is(equalTo(FileOperation.decryption)));
    assertThat(UI.getType(), is(equalTo(FileType.FILE)));
    assertThat(UI.getMethod(), is(equalTo(SynchronizationMethod.SYNC)));


}

@After
public void cleanUpStreams() {
    System.setOut(oldStdOut);
    System.setErr(oldStdErr);
    System.setIn(oldStdIn);
}
}

注意1:FileOperation、FileType和SynchronizationMethod都是获取1或0的枚举

同步方法示例:

public enum SynchronizationMethod {
SYNC(1), ASYNC(0);

private int method;

private SynchronizationMethod(int meth) {
    this.method = meth;
}

public static SynchronizationMethod fromInt(int meth) {
    for (SynchronizationMethod SM : SynchronizationMethod.values()) {
        if (SM.method == meth) {
            return SM;
        }
    }
    throw new IllegalArgumentException("No constant with method " + meth + " found");
}

public String toString(){
    if (method == 1){
        return "Sync";
    }
    else if(method == 0){
        return "ASync";
    }
    else{
        throw new IllegalArgumentException("No constant with method " + method + " found in toString");
    }
}
}

解决方案

public @Getter class UserInput {
    FileType type;
    FileOperation operation;
    SynchronizationMethod method;
    String path;
    private static InputReader in = new InputReader();

    public UserInput() {
        // Dont need to do anything
    }
问题出在构造函数中的类InputReader中

    public InputReader() {
    reader = new BufferedReader(new InputStreamReader(System.in));
    tokenizer = null;
}
此函数中的读取器和Junit函数中的输入流断开,正如这里的注释所建议的那样猜测-您的UserInput类说:

private static InputReader in = new InputReader();
而您的测试用例说:

String data = "0" + "\n0" + "\n0";
System.setIn(new ByteArrayInputStream(data.getBytes()));

换言之:可能存在断开连接。根据
InputReader
背后的实现,您可能只是从错误的源读取数据。

好的,我更改了itUnrelated:我建议不要将枚举基于整数。枚举已经有了从字符串构建常量的方法。你在这里发明轮子。您可以决定将枚举值表示为小写字符串,从“SYNC”创建SYNC(反之亦然)的代码应该更短!严肃地说:无论如何,当你要求用户输入时,不要使用数字。为什么希望用户记住0是异步的。让我让他说“异步”吧!我给出了一个答案,但问题是:你没有提供一个答案。我们不可能重新处理您的问题,因为您只有部分代码产生了问题。如果我的回答有帮助的话,很好。如果没有:请阅读该链接,并张贴符合这些要求的代码!例如,通过对Java Enum现有方法进行一些研究。更重要的是:用户不犯错误,或者他节省了输入几个字符?