Skip to content

Instantly share code, notes, and snippets.

@manuelleduc
Last active March 6, 2019 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelleduc/62c8efba51e22513082159a138e73c87 to your computer and use it in GitHub Desktop.
Save manuelleduc/62c8efba51e22513082159a138e73c87 to your computer and use it in GitHub Desktop.
package fr.mleduc.dynamictests;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Collection;
import java.util.stream.Stream;
public class DynamicFileTest {
@TestFactory
Stream<DynamicTest> dynamicTests() {
// expected final result
final File expectedDir = new File("src/test/resources/dirA");
// obtained result
final File resultDir = new File("src/test/resources/dirB");
final Collection<File> files = FileUtils.listFiles(expectedDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
return files.stream().map(f -> DynamicTest.dynamicTest(f.getName(),
() -> {
final Path relative = expectedDir.toPath().relativize(f.toPath());
final String relativePath = relative.toFile().getPath();
final File bfile = new File(resultDir, relativePath);
if (bfile.exists()) {
final Charset charset = Charset.defaultCharset();
final String expected = FileUtils.readFileToString(f, charset);
final String result = FileUtils.readFileToString(bfile, charset);
Assertions.assertEquals(expected, result);
} else {
Assertions.fail(relativePath + " expected to exist");
}
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment