簡體   English   中英

如何將數據傳遞給 Junit 中的測試類

[英]How to pass data to test class in 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();
        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");
    }
}

我不知道如何將數據傳遞給 String path = scanner.nextLine(); 這條線

我會從外部將掃描器或輸入流注入到被測類中,或者將其作為方法參數傳遞,因為它是類 ReadFilePath 的依賴項。

如果您在 ReadFilePath 類或其父類中完成了new Scanner(System.in) ,則無法模擬或替換它。

掃描儀類是最后一類,所以模擬不是我們的選擇。

您可以更新您的類以將 InputStream 帶入構造函數或方法參數,如下所示。

以 InputStream 為參數的代碼:

public class ReadFilePath {

  public String perform(InputStream is){
    Scanner scanner = new Scanner(is);
    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();
    }

  }
}

測試可以是這樣的

  @Test
  public void test() throws Exception {
    String inputForTest = "input";
    InputStream is = new ByteArrayInputStream(inputForTest.getBytes());
    ReadFilePath readFilePath = new ReadFilePath();
    String result = readFilePath.perform(is);
    //assert result
  }

如果您不調用String path = scanner.nextLine();那就太好了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();
         }
    }
}

您可以像下面這樣編寫測試用例:

public class ReadFilePathTest {

    @Test
    public void sampleTest() throws Exception{
        String data = "testValueHere";
        InputStream stdin = System.in;
        ReadFilePath obj=new ReadFilePath();
        try {
          System.setIn(new ByteArrayInputStream(data.getBytes()));
          Scanner scanner = new Scanner(System.in);
          String path=scanner.nextLine();
          Assert.assertEquals("testValueHere",obj.perform(path));
        } finally {
          System.setIn(stdin);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM