Search code examples
javajunitmockingmockitojunit4

How should I test this method with JUNIT and Mockito


I'm new to Junit and Mockito unit testing. I need to unit test the method "processFiles(final File folder)"

When I run the test I get a NullPointerException at "if (!this.studentService.isFileOk(data))".

Can you help me to how I should be testing this method properly. Thanks a lot in advance.

This is a part of the code from the processFiles method:

@Stateless
public class ListFilesService{
 
  @EJB
  private transient StudentService studentService;
 
 public void processFiles(final File folder)
    {
        File[] fileNames = folder.listFiles();
        List<String> lines = new ArrayList<>();
        
        try
        {
                    List<String> l = readContent(file);
                    l.forEach(i -> lines.add(i));

            String[] data = lines.get(0).trim().split(";");
            if (!this.studentService.isFileOk(data))
            {
                LOG.warning(String.format("File not valid"));
            }
            else
            {
                studentService.storeStudents(lines);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

And this is how I'm trying to test my processFile method

 

public class TestCases
{
 
 
    @Mock
    ListFilesService listFilesService;
    
    @Mock
    public StudentService studentService;

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
 
 @Test
    public void testWrite2() throws IOException
    {

        final File tempFile1 = temporaryFolder.newFile("tempFile.txt");
        final File tempFile2 = temporaryFolder.newFile("tempFile2.txt");

        String[] data = {"LastName", "FirstName", "Age"};


        listFilesService = new ListFilesService();
        studentService = Mockito.mock(StudentService.class);
        when(studentService.isFileOk(eq(data))).thenReturn(true);


        FileUtils.writeStringToFile(tempFile1, "LastName;FirstName;Age" +
                "\nxxx1;nnnn1;15", "UTF-8");
        FileUtils.writeStringToFile(tempFile2, "LastName;FirstName;Age" +
                "\nxxx2;nnnn2;19", "UTF-8");


        listFilesService.processFiles(temporaryFolder.getRoot());

    }

Solution

  • You're creating the ListFilesService object by hand, not letting Mockito inject the mocks (specifically - studentService):

    listFilesService = new ListFilesService();
    

    When creating the object like this, the field studentService remains null, because it is not initialized in any way and it causes the application to fail with NullPointerException.

    What you probably want to do is start your test class like this:

    @Mock
    private StudentService studentService;
    @InjectMocks
    private ListFilesService listFilesService;
    

    and then call MockitoAnnotations.initMocks(this) (read more here).

    Mocks initialization should be done before the test runs, so it's best to use a set up method with @Before annotation (read more here).

    Remember to use the listFilesService field in your test and not create the object by new!