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