scanner.throwfor()带有模拟输入和输出

发布于 2025-02-09 19:01:14 字数 2284 浏览 0 评论 0原文

我有几个测试的课程,如果单独运行。相反,如果我运行整个测试课,只有一个通过。

我正在测试输出(即字符串)等于预期的字符串。输出是通过方法产生的,该方法需要用户键盘输入。在每个测试中,我都会使用以下类模拟用户输入:

import java.io.*;
import java.nio.charset.Charset;

public class MockInOut {
    private PrintStream orig;
    private InputStream irig;
    private ByteArrayOutputStream os;
    private ByteArrayInputStream is;

    private final static Charset charset = Charset.defaultCharset();

    public MockInOut(String input) {
        orig = System.out;
        irig = System.in;

        os = new ByteArrayOutputStream();
        try {
            System.setOut(new PrintStream(os, false, charset.name()));
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }

        is = new ByteArrayInputStream(input.getBytes());
        System.setIn(is);
    }

    /**
     * Returns everything written to System.out since this {@code MockInOut} was
     * constructed. Can't be called on a closed {@code MockInOut}
     */
    public String getOutput() {
        if (os != null) {
            try {
                return os.toString(charset.name()).replace("\r\n", "\n");
            } catch (UnsupportedEncodingException ex) {
                throw new RuntimeException(ex);
            }
        } else {
            throw new Error("getOutput on closed MockInOut!");
        }
    }

    /**
     * Restores System.in and System.out
     */
    public void close() {
        os = null;
        is = null;
        System.setOut(orig);
        System.setIn(irig);
    }
}

每个测试的结构如下:

@Test
MyClassForConstructor myclassfc = new MyClassForConstructor();
MyClass myclass = new MyClass(myclassfc);

MockInOut userkeyboardinput = new MockInOut("firstLineInput\nsecondLineInput\nthirdLineInput\n");

MyObject myobToGetTested = myclass.methodThatRequiresInput();

MyObject myobExpected = new MyObject(String par1, String par2, String par3);

assertEquals(myobExpected.toString(), myobToGetTested.toString());

userkeyboardinput.close();

每个测试都具有相同的结构,并且更改的是键盘输入和创建预期对象的参数。

每次测试都在scanner.throwfor()上抛出nosuchelementException(),因为needInput = false,在scanner.next()上,除了一个测试(总是相同的,但与其他测试没有明确的差异:在通过的测试中,我看到了,我看到了需要设置的需求设置为真,但我不知道原因)。

我真的不知道还能做什么,感谢所有会提供帮助的人!

I have a class with several tests, which if run alone pass. Instead, if I run the entire test class, only one passes.

I am testing if the output (ie a String) is equal to an expected String. The output is produced by a method, which requires user keyboard input. In each test, I mock the user input with the following class:

import java.io.*;
import java.nio.charset.Charset;

public class MockInOut {
    private PrintStream orig;
    private InputStream irig;
    private ByteArrayOutputStream os;
    private ByteArrayInputStream is;

    private final static Charset charset = Charset.defaultCharset();

    public MockInOut(String input) {
        orig = System.out;
        irig = System.in;

        os = new ByteArrayOutputStream();
        try {
            System.setOut(new PrintStream(os, false, charset.name()));
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }

        is = new ByteArrayInputStream(input.getBytes());
        System.setIn(is);
    }

    /**
     * Returns everything written to System.out since this {@code MockInOut} was
     * constructed. Can't be called on a closed {@code MockInOut}
     */
    public String getOutput() {
        if (os != null) {
            try {
                return os.toString(charset.name()).replace("\r\n", "\n");
            } catch (UnsupportedEncodingException ex) {
                throw new RuntimeException(ex);
            }
        } else {
            throw new Error("getOutput on closed MockInOut!");
        }
    }

    /**
     * Restores System.in and System.out
     */
    public void close() {
        os = null;
        is = null;
        System.setOut(orig);
        System.setIn(irig);
    }
}

Each test is structured as follows:

@Test
MyClassForConstructor myclassfc = new MyClassForConstructor();
MyClass myclass = new MyClass(myclassfc);

MockInOut userkeyboardinput = new MockInOut("firstLineInput\nsecondLineInput\nthirdLineInput\n");

MyObject myobToGetTested = myclass.methodThatRequiresInput();

MyObject myobExpected = new MyObject(String par1, String par2, String par3);

assertEquals(myobExpected.toString(), myobToGetTested.toString());

userkeyboardinput.close();

Each test has the same structure and what it changes is the keyboard input and the parameters to create the expected object.

Every test throws a NoSuchElementException at Scanner.throwFor() because needInput = false, at Scanner.next(), except for one test (always the same, but which has no explicit difference with the others: in the test that passes, I saw that needInput is set true, but I don't know the reason).

I really don't know what else could be done, thanks for everybody who will help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文