...
Code Block |
---|
package com.celestial.mockito.filetodb; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Filesstatic org.junit.jupiter.api.Assertions.assertEquals; import javaorg.niojunit.file.Paths; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Test; import static org.junit.Assert.*; /** * * @author selvy */ public class LiveFileLoaderTest { // The inital design is described in this test /* The weakness should be obvious? The file to be loaded and it's location I use a shared network drive to run this code from different machines, normally dev-ing from a PC. When I ran the code on the laptop from a cafe it immediately failed because the C: on the laptop was completely different to the PC, so the original file C:/tmp/KeyboardHandler.txt did not exist THIS IS A GREAT EXAMPLE OF WHY THE UNIT TEST AND CUT SHOULD NOT BE STRONGLY LINKED TO ANY IO - NETWORK, DB, AND FILE SYSTEM */ @Test public void load_all_of_file_using_inbuilt_Files_type() { // arrange String fileToLoad = "c:/tmp/kipstor-settings.js"; FileLoader cut = new FileLoader(fileToLoad); int expectedBytesRead = 301; // act int bytesRead = cut.loadFile(fileToLoad); // assert assertEquals(expectedBytesRead, bytesRead); } } |
...
Code Block |
---|
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 { Stringprivate fileToLoad; class IntWrapper List<String> lines = Collections.emptyList();{ public public int value; } String fileToLoad; List<String> lines = Collections.emptyList(); public FileLoader(String fileToLoad) { this.fileToLoad = fileToLoad; } int loadFile(String fname) { try { lines = Files.readAllLines(Paths.get(fname), StandardCharsets.UTF_8); } catch (IOException e){} return calculateFileSize(); } private int calculateFileSize() { IntWrapper result = new IntWrapper(); lines.forEach(line -> { result.value += line.length(); }); return result.value; } } |
...
FileLoaderTest - suitable for a build pipeline and does not break the tenets of unit testing
Code Block |
---|
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ 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 java.util.logging.Level; import java.util.logging.Logger; import org.junit.Test; import static org.junit.Assert.*; 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) -> { List<String> result = null; try { result = Files.readAllLines(Paths.get(fname), StandardCharsets.UTF_8); } catch (IOException e){} return result; }); */ // 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); } // Redesign has worked so this test uses a stub to simulate a file being // loaded, it is passed in as lambda - We've decoupled ourselves from the // filesystem @Test public void load_all_of_file_via_stub() { // arrange String fileToLoad = "c:/tmp/KeyboardHandler.txt"; FileLoader cut = new FileLoader(fileToLoad); int expectedBytesRead = 10; // act int bytesRead = cut.loadFile((fname) -> { List<String> result = new ArrayList<>(); result.add("Hello"); result.add("world"); return result; }); // assert assertEquals(expectedBytesRead, bytesRead); } } |
...