Forráskód Böngészése

Stworzono test weryfikacji zachowań dla DirConverter

Adam Matuszewski 1 éve
szülő
commit
f5a714363a

+ 2 - 2
KonwersjaLiter2/src/main/java/pl/dmcs/DirConverter.java

@@ -11,8 +11,8 @@ import java.util.stream.Stream;
 public class DirConverter {
     private final FileConverter fileConverter;
 
-    public DirConverter() {
-        this.fileConverter = new FileConverter();
+    public DirConverter(FileConverter fileConverter) {
+        this.fileConverter = fileConverter;
     }
 
     public void convertDir(String directoryPath, String fileExtension) throws IOException {

+ 50 - 0
KonwersjaLiter2/src/test/java/pl/dmcs/DirConverterBehaviorTest.java

@@ -0,0 +1,50 @@
+package pl.dmcs;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.*;
+
+public class DirConverterBehaviorTest {
+    private static final String TEST_DIR = "target/test-classes/";
+    private static final String FILE_EXTENSION = ".txt";
+    private FileConverter fileConverter;
+    private DirConverter dirConverter;
+
+    @BeforeEach
+    void setUp() throws IOException {
+        Files.createDirectories(Paths.get(TEST_DIR));
+        Files.walk(Paths.get(TEST_DIR))
+                .filter(Files::isRegularFile)
+                .forEach(file -> {
+                    try {
+                        Files.delete(file);
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                });
+
+        Files.write(Paths.get(TEST_DIR, "file1.txt"), "hello world".getBytes());
+        Files.write(Paths.get(TEST_DIR, "file2.txt"), "hello world 2".getBytes());
+        Files.write(Paths.get(TEST_DIR, "file3.txt"), "hello world 3".getBytes());
+
+        fileConverter = mock(FileConverter.class);
+        dirConverter = new DirConverter(fileConverter);
+    }
+
+    @Test
+    void testConvertDir() throws IOException {
+        dirConverter.convertDir(TEST_DIR, FILE_EXTENSION);
+
+        verify(fileConverter).convertToUpperCase(Paths.get(TEST_DIR, "file1.txt").toString());
+        verify(fileConverter).convertToUpperCase(Paths.get(TEST_DIR, "file2.txt").toString());
+        verify(fileConverter).convertToUpperCase(Paths.get(TEST_DIR, "file3.txt").toString());
+    }
+
+}

+ 8 - 11
KonwersjaLiter2/src/test/java/pl/dmcs/DirConverterStateTest.java

@@ -15,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 public class DirConverterStateTest {
     private static final String TEST_DIR = "target/test-classes/";
     private static final String FILE_EXTENSION = ".txt";
+    private DirConverter dirConverter;
 
     @BeforeEach
     void setUp() throws IOException {
@@ -32,6 +33,9 @@ public class DirConverterStateTest {
         Files.write(Paths.get(TEST_DIR, "file1.txt"), "hello world".getBytes());
         Files.write(Paths.get(TEST_DIR, "file2.txt"), "hello world 2".getBytes());
         Files.write(Paths.get(TEST_DIR, "file3.txt"), "hello world 3".getBytes());
+
+        FileConverter fileConverter = new FileConverter();
+        dirConverter = new DirConverter(fileConverter);
     }
 
     private void assertFileContent(Path filePath, String expectedContent) throws IOException {
@@ -42,8 +46,7 @@ public class DirConverterStateTest {
 
     @Test
     void testConvertDir() throws IOException {
-        DirConverter converter = new DirConverter();
-        converter.convertDir(TEST_DIR, FILE_EXTENSION);
+        dirConverter.convertDir(TEST_DIR, FILE_EXTENSION);
 
         assertFileContent(Paths.get(TEST_DIR, "file1.txt"), "HELLO WORLD");
         assertFileContent(Paths.get(TEST_DIR, "file2.txt"), "HELLO WORLD 2");
@@ -55,18 +58,15 @@ public class DirConverterStateTest {
         Path emptyFilePath = Paths.get(TEST_DIR, "empty.txt");
         Files.write(emptyFilePath, new byte[0]);
 
-        DirConverter converter = new DirConverter();
-        converter.convertDir(TEST_DIR, FILE_EXTENSION);
+        dirConverter.convertDir(TEST_DIR, FILE_EXTENSION);
 
         assertFileContent(emptyFilePath, "");
     }
 
     @Test
     void testConvertDirNonExistentDirectory() {
-        DirConverter converter = new DirConverter();
-
         Exception exception = assertThrows(IOException.class, () -> {
-            converter.convertDir("non/existent/directory", FILE_EXTENSION);
+            dirConverter.convertDir("non/existent/directory", FILE_EXTENSION);
         });
 
         String expectedMessage = "non/existent/directory".replace("/", File.separator);
@@ -79,14 +79,11 @@ public class DirConverterStateTest {
     void testConvertDirNonMatchingExtension() throws IOException {
         Files.write(Paths.get(TEST_DIR, "file1.md"), "markdown content".getBytes());
 
-        DirConverter converter = new DirConverter();
-        converter.convertDir(TEST_DIR, FILE_EXTENSION);
+        dirConverter.convertDir(TEST_DIR, FILE_EXTENSION);
 
         assertFileContent(Paths.get(TEST_DIR, "file1.txt"), "HELLO WORLD");
         assertFileContent(Paths.get(TEST_DIR, "file2.txt"), "HELLO WORLD 2");
         assertFileContent(Paths.get(TEST_DIR, "file3.txt"), "HELLO WORLD 3");
         assertFileContent(Paths.get(TEST_DIR, "file1.md"), "markdown content");
-
-
     }
 }