Junit测试获取NoSuchElementException但仅限于特定数字

时间:2017-05-17 06:16:48

标签: java junit

我写了一些输入两个数字的代码,并检查它们是否为int,如果不清除键盘输入然后再次检查。这在JUnit测试中获得NoSuchElementException,但仅在特定输入值(5,而不是0)上获得。

import java.util.Scanner;

public class HasException {

    public int chooseNumber()
    {
     System.out.println("Enter a number, you can select 1 to 3");
        int number;
        do{
        Scanner in=new Scanner(System.in);
        while (!in.hasNextInt())    // when there isn't a integer next in the keyboard input
        {
            System.out.println("Not a number, please try again");
            String discardedString = in.next(); // empty the buffer
        }
        number =in.nextInt();
        if((number<1)||(number>3))
        {
            System.out.println("This is wrong choice, Please select 1 to 3");
        }
        }while((number<1)||(number>3));
        return number;
    }


}


import static org.junit.Assert.*;

import java.io.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class HasExceptionTest {

public final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
public final ByteArrayOutputStream errContent = new ByteArrayOutputStream();


HasException myHasException;    // declare the class to be tested

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() {
    System.setOut(new PrintStream(outContent));
    System.setErr(new PrintStream(errContent));

    myHasException = new HasException();

}


@After
public void cleanUpStreams() {
    System.setOut(null);
    System.setErr(null);
}


@Test
public void test1() {   // this test fails with NoSuchElementException
    String input = "5" + "2";
    InputStream in = new ByteArrayInputStream(input.getBytes());
    System.setIn(in);

    int result = myHasException.chooseNumber();  // exception NoSuchElementException here
    assertEquals(result, 2);
}

@Test
public void test2() {   // this test passes
    String input = "0" + "2";
    InputStream in = new ByteArrayInputStream(input.getBytes());
    System.setIn(in);

    int result = myHasException.chooseNumber();
    assertEquals(result, 2);
}



}

2 个答案:

答案 0 :(得分:4)

String input = "0" + "2";会产生值“02”。

String input = "5" + "2";将“52”分配给输入。

所以你只检查一个值。

如果你检查“02”,它会成功,你成功退出chooseNumber(),你的测试通过了。耶。

但是,如果输入“52”,则selectNumber()循环并尝试从“输入”中读取另一个数字。

由于您只有 ONE 值(“52”),因此第二次读取FAILS。使用NoSuchElementException。

答案 1 :(得分:1)

对于“52”,chooseNumber()循环尝试在第二次获取nextInt()并获得异常。所以你应该在没有下一个值时使用break。

chooseNumber()应如下所示: 公共类HasException {

public int chooseNumber() {
    System.out.println("Enter a number, you can select 1 to 3");
    int number = 0;
    do {
        Scanner in = new Scanner(System.in);
        System.out.println("log hasNext =" + in.hasNext());
        while (in.hasNext() && !in.hasNextInt()) // when there isn't a
                                                    // integer next in the
                                                    // keyboard input
        {
            System.out.println("log 2");

            System.out
                    .println("Not a number, please try again" + in.next());
            String discardedString = in.next(); // empty the buffer
        }

        if (!in.hasNext()) {
            break;
        }
        number = in.nextInt();

        if ((number < 1) || (number > 3)) {
            System.out
                    .println("This is wrong choice, Please select 1 to 3");
        }

    } while ((number < 1) || (number > 3));
    return number;
}

}