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