Reputation: 53
I am using Intellij IDEA 2019 and junit to read a bunch of files, process them, and see if I get the right result. Here is my code:
@Test
public void testSolution() {
Solver s;
Board b;
int counter = 0;
final File folder = new File("C:\\Users\\IdeaProjects\\EightPuzzle\\src\\ModifiedTests");
for (final File fileEntry : folder.listFiles()) {
System.out.println("processing file: " + fileEntry.getName());
In in = new In(fileEntry.getAbsolutePath());
int n = in.readInt();
int moves = in.readInt();
int[][] tiles = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
tiles[i][j] = in.readInt();
b = new Board(tiles);
s = new Solver(b);
assertEquals(s.moves(), moves);
counter++;
if (counter > 10) break;
}
}
Here is a sample file:
3
24
6 5 3
4 1 7
0 2 8
Is there any way I see which files have been processed, and what the result are? I have been trying to use the message field in the assertEquals method, but I do not know a way of accessing the result. I may need a different method?
Upvotes: 1
Views: 71
Reputation: 356
If I understand your problem correct, maybe parameterized tests are your solution, with the files in your directory being the parameters. Please check:
https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html
Upvotes: 1