Last active
March 6, 2019 17:42
-
-
Save manuelleduc/62c8efba51e22513082159a138e73c87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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