我試圖在JUnit 4.11中設置參數化測試,該參數化測試生成單個參數。然而,這個參數需要是一個數組本身。參數化JUnit測試中使用數組參數的反射異常
爲下面的代碼段的預期的行爲將是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$1.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$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.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$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.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)
一如往常:郵政堆棧跟蹤! – Seelenvirtuose
那麼......「不能通過修飾符_private_」訪問類org.mafagafogigante.dungeon.entity.creatures.HeroTest的成員......「任何不清楚的地方? – Seelenvirtuose
哦,親愛的,我總是在Eclipse的Failure Trace中激活了「Filter Trace」。這就是我的困惑所在。現在看完整的蹤跡,這個錯誤是顯而易見的。感謝您的廣泛暗示。我編輯了軌跡以正確反映示例。 – Johnson