English 中文(简体)
单位:如何使用jUnit和Mockito撰写测试案例
原标题:Unit: How to write test case using jUnit and Mockito

我对莫克托和格鲁特大学以及整个TDD非常新,我试图学习正确的方式来开展TDD的工作。 比如,我需要夫妇开始大脑。 页: 1

因此,我有一个方法getNameInc(String dirPath, String filenName)。 因此,提供了一份像bankAccount.pdf的文档,如果在这种文件夹中,没有文件名称bankAccount.pdf,然后回到bankAccountAA.pdf。 如果有bankAccount.pdf,则return bankAccountBB.pdf。 <代码>increment:AA-ZZ。 到达<代码>Z时 然后,该编码重新编号为A。 我已经采用了这种方法的逻辑。 我如何用Mockiti和jUnit测试这一方法?

EDIT

Here is the class and methods that are involved.

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() will be called in the constructor to pre-generate the hash map

问题回答

我假定,你正在尝试测试你的方法。 当你打电话时,它会查阅你所具体说明的目录中的档案,并依据其发现,更正你的姓名。

为了使班级能够进行测试,你应当从档案系统的依赖程度中抽出,以便你能够模拟你想要的任何目录内容。 您的班子将接受这一接口作为依赖性的事例,并称其查找名录中的内容。 当你将这门课程用于真正的节目时,你将提供参加科索沃独立党档案系统的这一接口。 当你集体测试这一类别时,你将供应这一接口的磁基。

避免将过多的逻辑列入档案系统Impl类别,因为你可以撰写严格的单位测试。 把它放在档案系统周围,这样,所有的智慧 st子都放在你身上,你将为此撰写大量单位测试。

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...

For what you have described there I wouldn t bother with Mockito, there doesn t seem to be anything to mock (because it is easy to manipulate the file system).

I would test ... - What happens if I call getNameInc and there are no matching files already - What happens if I call getNameInc and there are files AA-YY there already - What happens if I call getNameInc and file ZZ is there already

The point of TDD though is that you should have already written these tests and then implemented your code to make the tests pass. So you won t really be doing TDD since you already have the code.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签