Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Junit测试不会获取任何uchelementException,但只会获取特定的数字_Java_Junit - Fatal编程技术网

Java Junit测试不会获取任何uchelementException,但只会获取特定的数字

Java Junit测试不会获取任何uchelementException,但只会获取特定的数字,java,junit,Java,Junit,我写了一些代码,输入两个数字,并检查它们是否为int,如果不是,则清除键盘输入,然后再次检查。这将在JUnit测试下获得一个NoSuchElementException,但仅针对特定的输入值(5,而不是0) import java.util.Scanner; 公共类有异常{ public int chooseNumber() { System.out.println(“输入一个数字,可以选择1到3”); 整数; 做{ 扫描仪输入=新扫描仪(系统输入); while(!in.hasNextInt(

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

import java.util.Scanner;
公共类有异常{
public int chooseNumber()
{
System.out.println(“输入一个数字,可以选择1到3”);
整数;
做{
扫描仪输入=新扫描仪(系统输入);
while(!in.hasNextInt())//当键盘输入中没有整数时
{
System.out.println(“不是数字,请重试”);
String discardedString=in.next();//清空缓冲区
}
number=in.nextInt();
如果((编号3))
{
System.out.println(“这是错误的选择,请选择1到3”);
}
}而((3号));
返回号码;
}
}
导入静态org.junit.Assert.*;
导入java.io.*;
导入org.junit.After;
导入org.junit.AfterClass;
导入org.junit.Before;
导入org.junit.BeforeClass;
导入org.junit.Test;
公共类有ExceptionTest{
public final ByteArrayOutputStream outContent=new ByteArrayOutputStream();
public final ByteArrayOutputStream errContent=new ByteArrayOutputStream();
HasException myHasException;//声明要测试的类
@课前
public static void setUpBeforeClass()引发异常{
}
@下课
public static void tearDownAfterClass()引发异常{
}
@以前
公共作废设置(){
系统放样(新打印流(输出内容));
System.setErr(新打印流(errContent));
myHasException=新的HasException();
}
@之后
上游公共空间清理(){
系统放样(空);
System.setErr(空);
}
@试验
public void test1(){//此测试失败,且无任何异常
字符串输入=“5”+“2”;
InputStream in=newbytearrayinputstream(input.getBytes());
系统设置(in);
int result=myhaseexception.chooseNumber();//这里的异常NoSuchElementException
资产质量(结果2);
}
@试验
public void test2(){//此测试通过
字符串输入=“0”+“2”;
InputStream in=newbytearrayinputstream(input.getBytes());
系统设置(in);
int result=myHasException.chooseNumber();
资产质量(结果2);
}
}
字符串输入=“0”+“2”产生值“02”

String input=“5”+“2”为输入指定“52”

所以你只检查了一个值

如果您检查“02”,它将成功,您将成功退出chooseNumber(),并且您的测试将通过。耶

但是,如果输入“52”,请选择number()循环并尝试从“input”中读取另一个数字

因为只有一个值(“52”),所以第二次读取失败。带有NoTouchElementException。

String input=“0”+“2”产生值“02”

String input=“5”+“2”为输入指定“52”

所以你只检查了一个值

如果您检查“02”,它将成功,您将成功退出chooseNumber(),并且您的测试将通过。耶

但是,如果输入“52”,请选择number()循环并尝试从“input”中读取另一个数字

因为只有一个值(“52”),所以第二次读取失败。使用NoTouchElementException。

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

ChooseSNumber()应如下所示: 公共类有异常{

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;
}
public int chooseNumber(){
System.out.println(“输入一个数字,可以选择1到3”);
整数=0;
做{
扫描仪输入=新扫描仪(系统输入);
System.out.println(“log hasNext=“+in.hasNext());
while(in.hasNext()&&!in.hasNextInt())//当没有
//下一个整数
//键盘输入
{
System.out.println(“日志2”);
系统输出
.println(“不是数字,请重试”+in.next());
String discardedString=in.next();//清空缓冲区
}
如果(!in.hasNext()){
打破
}
number=in.nextInt();
如果((数字<1)| |(数字>3)){
系统输出
.println(“这是错误的选择,请选择1到3”);
}
}而((数<1)| |(数>3));
返回号码;
}
}

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

ChooseSNumber()应如下所示: 公共类有异常{

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;
}
public int chooseNumber(){
System.out.println(“输入一个数字,可以选择1到3”);
整数=0;
做{
扫描仪输入=新扫描仪(系统输入);
System.out.println(“log hasNext=“+in.hasNext());
while(in.hasNext()&&!in.hasNextInt())//当没有
//下一个整数
//键盘输入
{
System.out.println(“日志2”);
系统输出
.println(“不是数字,请重试”+in.next());
String discardedString=in.next();//清空缓冲区
}
如果(!in.hasNext()){
打破
}
number=in.nextInt();
如果((数字<1)| |(数字>3)){
系统输出
.println(“这是错误的选择,请选择1到3”);
}
}而((数<1)| |(数>3));
返回号码;
}

}

提示:查看您的命名技巧
HasException
对于这个类来说是一个非常糟糕的名字。此类要求用户提供特定的输入。那没什么大不了的