Unit: How to write a test case using jUnit and Mockito

I am very new to Mockito and jUnit and TDD in general, and I am trying to learn the right way to do TDD. I need couples to start my brain. Please help me

So, I have a getNameInc(String dirPath, String filenName) method getNameInc(String dirPath, String filenName) . Therefore, a file_name of type bankAccount.pdf , and if there is no bankAccount.pdf file name in this folder, return bankAccountAA.pdf . If there is one bankAccount.pdf , then return bankAccountBB.pdf increment is AA-ZZ . When he reaches ZZ , he will return to AA . I already implement the logic of this method. How to use unit test this method using Mockiti and jUnit?

EDIT

Here are 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 mapping

+4
source share
2 answers

You are trying to test the getNameInc (..) method, I guess. When you call it, it searches for files in the directory you specified and, based on what it finds, decorates the name you gave it.

To make a test class a class, you must abstract the dependency on the file system so that in the layout you can simulate the entire contents of the directory that you want. Your class will take an instance of this interface as a dependency and call it to find out what is in the directory. When you really use the class in your program, you will provide an implementation of this interface that delegates calls to the JDK file system. When you test the class, you will supply the Mockito mocks of this interface.

Avoid unnecessary logic in the FilesystemImpl class, since you cannot write a strict unit test for it. Store it in a very simple wrapper around the file system, so that all the smart things are in your class, in which you will write many unit tests for.

 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); } } 

Since there is a lot of duplication between tests, you will probably create an installation method and do some work there and create some instance variables to use the tests:

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

For what you described there, I would not worry about Mockito, it seems that there is nothing to mock something (because it is easy to manipulate the file system).

I would experience ... - What happens if I call getNameInc and there are no corresponding files anymore - What happens if I call getNameInc and there are AA-YY files there already - What happens if I call getNameInc and the ZZ file already exists

The point of TDD, however, is that you should have written these tests already and then implemented your code to pass the tests. That way, you really won't do TDD, since you already have the code.

+5
source

All Articles