使用数组参数的参数化 JUnit 测试中的反射异常
Reflection Exception in parameterized JUnit test using array parameter
我试图在 JUnit 4.11 中设置一个参数化测试,它生成一个 单个 参数。但是,此参数本身需要是一个数组。
以下代码片段的预期行为是 test()
是 运行 两次,而 arguments
在第一个 运行 中包含一个数组 {"test1", "test2"}
和第二个数组 {"test3", "test4"}
运行.
@RunWith(Parameterized.class)
public class Tester {
@Parameter
private String[] arguments;
@Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(
new Object[][]{
{new String[] {"test1","test2"}},
{new String[] {"test3","test4"}}
}
);
}
@Test
public void test() {
fail();
}
}
但是,当我执行测试时,抛出了 SlowCheckMemberAccess
异常。 Google 没有针对此类问题提供任何结果。
谁能解释这里出了什么问题?
未经过滤的 JUnit 跟踪是:
java.lang.IllegalAccessException: Class org.junit.runners.Parameterized$TestClassRunnerForParameters can not access a member of class org.mafagafogigante.dungeon.entity.creatures.Tester with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.checkAccess(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTestUsingFieldInjection(Parameterized.java:201)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:179)
at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我认为您不需要 Arrays.asList()
,因为数组也是 Iterable
。
如果你给它指定一个数组参数,Arrays.asList()
也会被愚弄。如果要使用它,请添加显式转换。
用@Parameter
注释的字段必须是public
。
JUnit 4.13 will have an improved error message:
Cannot set parameter 'arguments'. Ensure that the field 'arguments' is public.
我试图在 JUnit 4.11 中设置一个参数化测试,它生成一个 单个 参数。但是,此参数本身需要是一个数组。
以下代码片段的预期行为是 test()
是 运行 两次,而 arguments
在第一个 运行 中包含一个数组 {"test1", "test2"}
和第二个数组 {"test3", "test4"}
运行.
@RunWith(Parameterized.class)
public class Tester {
@Parameter
private String[] arguments;
@Parameters
public static Iterable<Object[]> data() {
return Arrays.asList(
new Object[][]{
{new String[] {"test1","test2"}},
{new String[] {"test3","test4"}}
}
);
}
@Test
public void test() {
fail();
}
}
但是,当我执行测试时,抛出了 SlowCheckMemberAccess
异常。 Google 没有针对此类问题提供任何结果。
谁能解释这里出了什么问题?
未经过滤的 JUnit 跟踪是:
java.lang.IllegalAccessException: Class org.junit.runners.Parameterized$TestClassRunnerForParameters can not access a member of class org.mafagafogigante.dungeon.entity.creatures.Tester with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(Unknown Source)
at java.lang.reflect.AccessibleObject.checkAccess(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTestUsingFieldInjection(Parameterized.java:201)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.createTest(Parameterized.java:179)
at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runners.Suite.runChild(Suite.java:127)
at org.junit.runners.Suite.runChild(Suite.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:53)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我认为您不需要 Arrays.asList()
,因为数组也是 Iterable
。
如果你给它指定一个数组参数,Arrays.asList()
也会被愚弄。如果要使用它,请添加显式转换。
用@Parameter
注释的字段必须是public
。
JUnit 4.13 will have an improved error message:
Cannot set parameter 'arguments'. Ensure that the field 'arguments' is public.