簡體   English   中英

單位:如何使用jUnit和Mockito編寫測試用例

[英]Unit: How to write test case using jUnit and Mockito

我對Mockito和jUnit以及TDD一般都是新手,我嘗試學習正確的TDD方法。 我需要幾個例子才能啟動我的大腦。 所以請幫助我

所以我有一個方法getNameInc(String dirPath, String filenName) 所以給定一個像bankAccount.pdf這樣的fileName,如果在這個文件夾中沒有文件名bankAccount.pdf ,那么返回bankAccountAA.pdf 如果存在一個bankAccount.pdfreturn bankAccountBB.pdf incrementAA-ZZ 當它到達ZZ然后它回滾到AA 我已經實現了這個方法的邏輯。 如何使用Mockiti和jUnit對此方法進行單元測試?

編輯

這是涉及的類和方法。

public class PProcessor{

    private final Map<Integer, String> incMap = new HashMap<Integer, String>();

    private String getNameInc(String dirPath, String filenName){
         String[] nameList = new File(dirPath).list(new FilenameFilter(){
            public boolean accept(File file, String name) {
                //only load pdf files
                return (name.toLowerCase().endsWith(".pdf"));
            }
        });
        //Return the number of occurance that a given file name appear
        //inside the output folder.
        int freq = 0;
        for(int i=0; i<nameList.length; i++){

            if(fileName.equals(nameList[i].substring(0, 8))){
                freq++;
            }
        }
        return incMap.get(freq);
    }

    private void generateIncHashMap(){
        incMap.put(new Integer(0), "AA");
        incMap.put(new Integer(1), "BB");
        incMap.put(new Integer(2), "CC");
        ...
    }
}

將在構造函數中調用generateIncHashMap()以預生成哈希映射

您正在嘗試測試您的getNameInc(..)方法。 當你調用它時,它會查找你指定的目錄中的文件,並根據它找到的內容裝飾你給它的名字。

要使類可單元測試,您應該抽象對文件系統的依賴,以便在模擬中,您可以模擬您想要的任何目錄內容。 您的類將接受此接口的實例作為依賴項,並調用它以查找目錄中的內容。 當您在程序中使用該類時,您將提供此接口的實現,該實現委派給JDK文件系統調用。 當您對該類進行單元測試時,您將提供此接口的Mockito模擬。

避免在FilesystemImpl類中添加太多邏輯,因為您無法為其編寫嚴格的單元測試。 保持它是一個非常簡單的文件系統包裝器,所以所有智能的東西都在Yourclass中,你將為它編寫大量的單元測試。

public interface Filesystem {
    boolean contains(String dirpath, String filename);
}

public class FilesystemImpl {
    boolean contains(String dirpath, String filename) {
        // Make JDK calls to determine if the specified directory has the file.
        return ...
    }
}

public class Yourmainclass {
    public static void main(String[] args) {

         Filesystem f = new FilesystemImpl();
         Yourclass worker = new Yourclass(f);
         // do something with your worker
         // etc...
    }
}

public class Yourclass {
    private Filesystem filesystem;

    public Yourclass(Filesystem filesystem) {
        this.filesystem = filesystem;
    }

    String getNameInc(String dirpath, String filename) {
       ...
       if (filesystem.contains(dirpath, filename) {
          ...
       }
    }

}

public class YourclassTest {

   @Test
   public void testShouldAppendAAWhenFileExists() {
       Filesystem filesystem = Mockito.mock(Filesystem.class);
       when(filesystem.contains("/some/mock/path", "bankAccount.pdf").thenReturn(true);
       Yourclass worker = new Yourclass(filesystem);
       String actual = worker.getNameInc("/some/mock/path", "bankAccount.pdf");
       assertEquals("bankAccountAA.pdf", actual);
   }

   @Test
   public void testShouldNotAppendWhenFileDoesNotExist {
       Filesystem filesystem = Mockito.mock(Filesystem.class);
       when(filesystem.contains("/some/mock/path", "bankAccount.pdf").thenReturn(false);
       Yourclass worker = new Yourclass(filesystem);
       String actual = worker.getNameInc("/some/mock/path", "bankAccount.pdf");
       assertequals("bankAccount.pdf", actual);
   }
}

由於測試之間存在大量重復,您可能會創建一個設置方法並在那里完成一些工作,並為要使用的測試創建一些實例變量:

    private static final String TEST_PATH = "/some/mock/path";
    private static final String TEST_FILENAME = "bankAccount.pdf";
    private Filesystem filesystem;
    private Yourclass worker;

    @Before
    public void setUp() {
        filesystem = Mockito.mock(Filesystem.class);
        worker = new Yourclass(filesystem);
    }

    @Test
   public void testShouldAppendAAWhenFileExists() {
       when(filesystem.contains(TEST_PATH, TEST_FILENAME).thenReturn(true);
       String actual = worker.getNameInc(TEST_PATH, TEST_FILENAME);
       assertEquals("bankAccountAA.pdf", actual);
   }

   etc...

對於你在那里描述的內容,我不打擾Mockito,似乎沒有什么可以模擬的(因為它很容易操作文件系統)。

我會測試... - 如果我調用getNameInc並且沒有匹配的文件會發生什么 - 如果我調用getNameInc並且已經存在文件AA-YY會發生什么 - 如果我調用getNameInc並且文件ZZ已經存在會發生什么

TDD的重點是您應該已經編寫了這些測試,然后實現了代碼以使測試通過。 因為你已經有了代碼,所以你不會真正做TDD。

暫無
暫無

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

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