Versions Compared

Key

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

...

The Unit Test

Code Block
languagec#
using NUnit.Framework;
using file_loader_service;
using System.Collections.Generic;
using System.IO;

namespace file_loader_tests
{
    public class FileLoaderTests
    {
        [Test]
        public void load_all_of_file_using_inbuilt_Files_typepackage 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;

/**
 *
 * @author selvy
 */
public class FileLoaderTest 
{
   // To use a different type of file system loader, pass a lambda to loadFile()
   // as shown here
 {  /*
       int bytesRead = cut.loadFile((fname) ->
    // arrange  {
          stringList<String> fileToLoadresult = "c:/tmp/KeyboardHandler.java.txt";null;
          try
       FileLoader cut = new{
FileLoader();             int expectedBytesReadresult = 1383Files.readAllLines(Paths.get(fname), StandardCharsets.UTF_8);
          }
  //   act     catch (IOException e){}
      int   bytesRead = cut.LoadFile(fileToLoad)return result;
       });
   */

    // assertRedesign the FileLoader so that the machenism to load files up can be Assert.AreEqual(expectedBytesRead,
bytesRead);    // passed in as a }lambda - still titghtly coupled the file system
  [Test]    @Test
    public void load_all_of_file_using_inbuilt_Files_type_viaas_lambda()
   
    {
 
          // arrange
            stringString fileToLoad = "c:/tmp/KeyboardHandler.java.txt";
            FileLoader cut = new FileLoader(fileToLoad);
        int expectedBytesRead = 10; int expectedBytesRead = 1383//1371;
        List<String> pretendFileContent = new ArrayList<>();
// act       pretendFileContent.add("Hello");
     int bytesRead = cutpretendFileContent.LoadFile(fileToLoad, (fname) =>add("world");
        MockedStatic<Files> ff = Mockito.mockStatic(Files.class);
        ff.when(() -> Files.readAllLines(Paths.get(fileToLoad), StandardCharsets.UTF_8)).thenReturn(pretendFileContent);

 {       // act
        IEnumerable<string>int resultbytesRead = null;
cut.loadFile((fname) ->
        {
      try     List<String> result = null;
        {   try
           {
     result = File.ReadLines(fname);        result = Files.readAllLines(Paths.get(fname), StandardCharsets.UTF_8);
      }     }
           catch (IOException e) { }
  
             return result;
            });


           // assert
            Assert.AreEqualassertEquals(expectedBytesRead, bytesRead);
        }
    }
}

The CUT FileLoader

Code Block
languagec#
using System;
using System.Collections.Generic;
using System.IO;

namespace file_loader_service
{
    public delegate IEnumerable<string> ILoadFile(String fname);

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


/**
 *
 * @author selvy
 */
public class FileLoader 
{
  {  class   IntWrapper
   IEnumerable<string> lines{
= new List<string>();      int value;
  public FileLoader() }
    
  {  String fileToLoad;
    List<String> }lines = Collections.emptyList();

    public FileLoader(String publicfileToLoad) int
LoadFile(string fname, ILoadFile func) {
       { this.fileToLoad = fileToLoad;
    }

   lines =int funcloadFile(String fname); 
    {
      return CalculateFileSize(); try
       } {
        public int LoadFile(string fname) lines = Files.readAllLines(Paths.get(fname), StandardCharsets.UTF_8);
    {    }
        trycatch (IOException e)
        {
 {       }

        lines = File.ReadLines(fname);
 return calculateFileSize();
    }    
    
 }   public List<String> getLines() {
      catch (IOException e)return {lines;
}    }
    
    returnint CalculateFileSizeloadFile();ILoader func) 
    {
        }lines = func.loadFile(fileToLoad);
       private intreturn CalculateFileSizecalculateFileSize();
    }    {
    
    private int calculateFileSize()
int result = 0; {
        IntWrapper result =  foreach (string line in lines)new IntWrapper();
        
        lines.forEach(line -> {
                result.value += line.Lengthlength();
        });
   };     
        return result;
        }.value;
    }
}