Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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中将数据传递给测试类_Java_Unit Testing_Junit - Fatal编程技术网

Java 如何在Junit中将数据传递给测试类

Java 如何在Junit中将数据传递给测试类,java,unit-testing,junit,Java,Unit Testing,Junit,我是JUnit新手,不知道如何测试这种代码 package input.commandline; import java.util.InputMismatchException; import java.util.regex.Pattern; public class ReadFilePath extends CommandLineInput{ public String perform(){ String path = scanner.nextLine()

我是JUnit新手,不知道如何测试这种代码

    package input.commandline;

import java.util.InputMismatchException;
import java.util.regex.Pattern;

public class ReadFilePath extends CommandLineInput{

    public String perform(){
        String path = scanner.nextLine();
        String regularExpression = "([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\?";
        boolean isMatched = Pattern.matches(regularExpression,path);
        if(isMatched){
            return path;
        }else {
            throw new InputMismatchException();
        }

    }
}
这是我的测试课

package input.commandline;

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class ReadFilePathTest {

    @Test
    public void sampleTest() throws Exception{
//        ReadFilePath readFile = new ReadFilePath();
//
//        readFile.perform();

        Assert.assertEquals("sam","sam");
    }
}
我不知道如何将数据传递给 字符串路径=scanner.nextLine();
这一行

我将从外部将扫描器或输入流注入到被测试的类中,或者将其作为方法参数传递,因为它是类ReadFilePath的依赖项

如果您在ReadFilePath类或其父类中创建了
新扫描仪(System.in)
,则无法对其进行模拟或替换

Scanner类是最后一个类,所以模拟不是我们的选择

您可以更新您的类以将InputStream引入构造函数或方法参数,如下所示

以InputStream为参数的代码:

公共类ReadFilePath{
公共字符串执行(InputStream为){
扫描仪=新扫描仪(is);
字符串路径=scanner.nextLine();
字符串正则表达式=“([a-zA-Z]:)?(\\[a-zA-Z0-9\-]+)+\?”;
布尔isMatched=Pattern.matches(正则表达式,路径);
如果(已匹配){
返回路径;
}否则{
抛出新输入失配异常();
}
}
}
测试可以是这样的

@测试
public void test()引发异常{
字符串inputForTest=“input”;
InputStream is=newbyteArrayInputStream(inputForTest.getBytes());
ReadFilePath ReadFilePath=新的ReadFilePath();
字符串结果=readFilePath.perform(is);
//断言结果
}

如果您不调用
String path=scanner.nextLine()直接在代码内部。您可能需要考虑重新配置代码以执行从一个单独的方法读取输入,以便您可以正确地测试您的功能并模拟输入控制台读取。

public class ReadFilePath{
    public void readAndPerform() {
        Scanner scanner=new Scanner(System.in);
        String path = scanner.nextLine();
        perform(path);
    }
    
    public String perform(String path){
         String regularExpression = "([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\?";
         boolean isMatched = Pattern.matches(regularExpression,path);
         if(isMatched){
             return path;
         }else {
             throw new InputMismatchException();
         }
    }
}
您可以编写测试用例,如下所示:

公共类ReadFilePathTest{
@试验
public void sampleTest()引发异常{
String data=“testValueHere”;
InputStream stdin=System.in;
ReadFilePath obj=新的ReadFilePath();
试一试{
setIn(新的ByteArrayInputStream(data.getBytes());
扫描仪=新的扫描仪(System.in);
字符串路径=scanner.nextLine();
Assert.assertEquals(“testValueHere”,obj.perform(path));
}最后{
系统设置(标准输入);
}
}
}

扫描仪对象来自何处?这可能有助于为您指明正确的方向: