junit java 错误消息的参数化测试构造函数:测试 class 应该只有一个 public 零参数构造函数
parameterized test constructor of junit java error message: Test class should have exactly one public zero-argument constructor
对于我尝试创建的这个参数化测试用例,我确实需要一些帮助。无论我创建哪种构造函数,IDE 都会给出错误消息。
这是我的代码:
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\Users\Azizam\IdeaProjects\EightPuzzle\src\ModifiedTests");
final static String destFolder = "C:\Users\Azizam\IdeaProjects\EightPuzzle\src\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
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();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
private Board board;
@Parameterized.Parameter(1)
private int expectedNumberOfMoves;
public SolverTest(Board board, int expectedNumberOfMoves) {
this.board = board;
this.expectedNumberOfMoves = expectedNumberOfMoves;
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
我尝试了不同的方法来创建 1 个参数、2 个和无参数构造函数。但我从未见过此类问题或解决方案。我正在关注 this link and this 教程。这是我的第一个参数化测试,调试似乎也没有为我提供太多帮助。
我还看到了 these 链接,但它们没有帮助。我也可以在 GitHub 或 gist 上提供项目其余部分的代码。我确实通过正确创建 fileList 来调试我的代码,但我对之后发生的事情或需要发生的事情知之甚少。以下是错误的摘录:
java.lang.Exception: Test class should have exactly one public zero-argument constructor
at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.validateConstructor(BlockJUnit4ClassRunnerWithParameters.java:90)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.<init>(BlockJUnit4ClassRunnerWithParameters.java:27)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory.createRunnerForTestWithParameters(BlockJUnit4ClassRunnerWithParametersFactory.java:16)
at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:313)
at org.junit.runners.Parameterized.<init>(Parameterized.java:248)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder.buildRunner(DefensiveAllDefaultPossibilitiesBuilder.java:113)
这是我的代码的最新版本:
package assignments;
import edu.princeton.cs.algs4.In;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.util.ArrayList;
import static junit.framework.TestCase.assertEquals;
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\Users\Azizam\IdeaProjects\EightPuzzle\src\ModifiedTests");
final static String destFolder = "C:\Users\Azizam\IdeaProjects\EightPuzzle\src\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
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();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
public Board board;
@Parameterized.Parameter(1)
public int expectedNumberOfMoves;
public SolverTest() {
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
这是调试会话的图片,显示了文件列表中我想要的所有内容。一些板对象如何不转移到我的规划求解构造函数。
这里是无参数构造函数错误的意思。
测试class中的构造函数如下:
public SolverTest(Board board, int expectedNumberOfMoves) {
this.board = board;
this.expectedNumberOfMoves = expectedNumberOfMoves;
}
它有 2 个参数,因此不是无参数构造函数。下面是一个无参数的构造函数:
public SolverTest() {
}
删除 2-arg 构造函数将起作用,因此不需要显式列出,因为 java 编译器会自动添加默认的无参数构造函数。
然而 错误的原因是 Parameterized
测试 class.
的 2 种方法的混合
要么使用带有 @Parameterized.Parameters
字段的无参数构造函数(顺便说一句,它需要是 public 而不是私有的),或者删除这些字段并使用带有参数的构造取参数
这是为使用第一种方法(即使用 @Parameterized.Parameters
字段)修改的代码:
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\Users\Azizam\IdeaProjects\EightPuzzle\src\ModifiedTests");
final static String destFolder = "C:\Users\Azizam\IdeaProjects\EightPuzzle\src\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
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();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
public Board board;
@Parameterized.Parameter(1)
public int expectedNumberOfMoves;
public SolverTest() {
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
对于我尝试创建的这个参数化测试用例,我确实需要一些帮助。无论我创建哪种构造函数,IDE 都会给出错误消息。 这是我的代码:
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\Users\Azizam\IdeaProjects\EightPuzzle\src\ModifiedTests");
final static String destFolder = "C:\Users\Azizam\IdeaProjects\EightPuzzle\src\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
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();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
private Board board;
@Parameterized.Parameter(1)
private int expectedNumberOfMoves;
public SolverTest(Board board, int expectedNumberOfMoves) {
this.board = board;
this.expectedNumberOfMoves = expectedNumberOfMoves;
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
我尝试了不同的方法来创建 1 个参数、2 个和无参数构造函数。但我从未见过此类问题或解决方案。我正在关注 this link and this 教程。这是我的第一个参数化测试,调试似乎也没有为我提供太多帮助。 我还看到了 these 链接,但它们没有帮助。我也可以在 GitHub 或 gist 上提供项目其余部分的代码。我确实通过正确创建 fileList 来调试我的代码,但我对之后发生的事情或需要发生的事情知之甚少。以下是错误的摘录:
java.lang.Exception: Test class should have exactly one public zero-argument constructor
at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.validateConstructor(BlockJUnit4ClassRunnerWithParameters.java:90)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.<init>(BlockJUnit4ClassRunnerWithParameters.java:27)
at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory.createRunnerForTestWithParameters(BlockJUnit4ClassRunnerWithParametersFactory.java:16)
at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:313)
at org.junit.runners.Parameterized.<init>(Parameterized.java:248)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveAnnotatedBuilder.buildRunner(DefensiveAllDefaultPossibilitiesBuilder.java:113)
这是我的代码的最新版本:
package assignments;
import edu.princeton.cs.algs4.In;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.util.ArrayList;
import static junit.framework.TestCase.assertEquals;
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\Users\Azizam\IdeaProjects\EightPuzzle\src\ModifiedTests");
final static String destFolder = "C:\Users\Azizam\IdeaProjects\EightPuzzle\src\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
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();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
public Board board;
@Parameterized.Parameter(1)
public int expectedNumberOfMoves;
public SolverTest() {
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}
这是调试会话的图片,显示了文件列表中我想要的所有内容。一些板对象如何不转移到我的规划求解构造函数。
测试class中的构造函数如下:
public SolverTest(Board board, int expectedNumberOfMoves) {
this.board = board;
this.expectedNumberOfMoves = expectedNumberOfMoves;
}
它有 2 个参数,因此不是无参数构造函数。下面是一个无参数的构造函数:
public SolverTest() {
}
删除 2-arg 构造函数将起作用,因此不需要显式列出,因为 java 编译器会自动添加默认的无参数构造函数。
然而 错误的原因是 Parameterized
测试 class.
要么使用带有 @Parameterized.Parameters
字段的无参数构造函数(顺便说一句,它需要是 public 而不是私有的),或者删除这些字段并使用带有参数的构造取参数
这是为使用第一种方法(即使用 @Parameterized.Parameters
字段)修改的代码:
@RunWith(Parameterized.class)
public class SolverTest {
final static File folder = new File("C:\Users\Azizam\IdeaProjects\EightPuzzle\src\ModifiedTests");
final static String destFolder = "C:\Users\Azizam\IdeaProjects\EightPuzzle\src\testresults";
final static ArrayList<Object[][]> filesList = new ArrayList<>();
final Object currentBoard = new Object();
@Parameterized.Parameters
public static Iterable<Object[][]> data() {
String path = "";
int counter = 0;
for (final File fileEntry : folder.listFiles()) {
//System.out.println("processing file: " + fileEntry.getName())
counter++;
if (counter == 20) break;
path = destFolder + fileEntry;
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();
Board b = new Board(tiles);
Object[][] fileList = new Object[][]{{b, moves}};
filesList.add(fileList);
}
return filesList;
}
@Parameterized.Parameter(0)
public Board board;
@Parameterized.Parameter(1)
public int expectedNumberOfMoves;
public SolverTest() {
}
@Test
public void test() {
assertEquals(expectedNumberOfMoves, new Solver(board).moves());
}
}