Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. In the //Act part of the test, we want to write something like this

  2. Code Block
    languagejava
            // act
            int bytesRead = cut.loadFile((fname) ->
            {
               List<String> result = null;
               try
               {
                   result = Files.readAllLines(Paths.get(fname), StandardCharsets.UTF_8);
               }
               catch (IOException e){}
               return result;
            });
  3. Reading the API guides we see that File.readAllLines() returns a List<string>, so let's change the code to match this

  4. Code Block
    languagejava
    package com.celestial.mockito.filetodb;
    
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    import static org.junit.jupiter.api.Assertions.assertEquals;
    import org.junit.jupiter.api.Test;
    import org.mockito.MockedStatic;
    import org.mockito.Mockito;
    
    public class FileLoaderTest 
    {
        // Redesign the FileLoader so that the machenism to load files up can be 
        // passed in as a lambda - still titghtly coupled the file system
        @Test
        public void load_all_of_file_using_inbuilt_Files_type_as_lambda() 
        {
            // arrange
            String fileToLoad = "c:/tmp/KeyboardHandler.txt";
            FileLoader cut = new FileLoader(fileToLoad);
            int expectedBytesRead = 10;    //1371;
            List<String> pretendFileContent = new ArrayList<>();
            pretendFileContent.add("Hello");
            pretendFileContent.add("world");
            MockedStatic<Files> ff = Mockito.mockStatic(Files.class);
            ff.when(() -> Files.readAllLines(Paths.get(fileToLoad), StandardCharsets.UTF_8)).thenReturn(pretendFileContent);
    
            // act
            int bytesRead = cut.loadFile((fname) ->
            {
               List<String> result = null;
               try
               {
                   result = Files.readAllLines(Paths.get(fname), StandardCharsets.UTF_8);
               }
               catch (IOException e){}
               return result;
            });
    
            // assert
            assertEquals(expectedBytesRead, bytesRead);
        }
    }
  5. At line 3228, we are using Mockito’s mockStatic() to mock a Static class

  6. At line 3329, we setup the expectation

  7. So we need to design a loadFile() method that takes as a parameter a block of code that can read the file from any source.

  8. public int LoadFile(string fname, ILoadFile func)

  9. Create a new interface called

  10. Code Block
    languagejava
    package com.celestial.mockito.filetodb;
    
    import java.util.List;
    
    /**
     *
     * @author selvy
     */
    @FunctionalInterface
    public interface ILoader 
    {
        List<String>    loadFile(String fname);
    }
  11. Modify FileLoader so it has this additional functionality

  12. Code Block
    languagejava
        public List<String> getLines() {
            return lines;
        }
        
        int loadFile(ILoader func) 
        {
            lines = func.loadFile(fileToLoad);
            return calculateFileSize();
        }    
    ...

...