Skip to content

Commit

Permalink
Add FileFilterTest unit tests
Browse files Browse the repository at this point in the history
Introduce unit tests to validate file and directory listing using FileFilter. Tests include anonymous inner class, expression lambda, block lambda, and a pre-assigned variable for filtering directories. Ensure the tests cover multiple approach for better code reliability.
  • Loading branch information
kousen committed Sep 17, 2024
1 parent f44bab8 commit cd369ff
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileFilter;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class FileFilterTest {
private final File root = new File("src/main/java");

@Test
void listFiles() {
File[] files = root.listFiles();
assert files != null;
for (File file : files) {
System.out.println(file);
}
assertEquals(22, files.length);
}

@Test
void listDirectories_anonInnerClass() {
File[] directories = root.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
assertEquals(14, directories.length);
}

@Test
void listDirectories_expressionLambda() {
File[] directories = root.listFiles(file -> file.isDirectory());
assertEquals(14, directories.length);
}

@Test
void listDirectories_blockLambda() {
File[] directories = root.listFiles(file -> {
System.out.println("Checking: " + file);
return file.isDirectory();
});
assertEquals(14, directories.length);
}

@Test
void listDirectories_assignedToVariable() {
FileFilter filter = (File file) -> file.isDirectory();
File[] directories = root.listFiles(filter);
assertEquals(14, directories.length);
}
}

0 comments on commit cd369ff

Please sign in to comment.