1

I tried to setup a parameterized test in JUnit 4.11 which generates a single parameter. This parameter, however, needs to be an array itself.

The expected behavior for the following code snippet would be that test() is run twice while arguments holds an array {"test1", "test2"} in the first run and an array {"test3", "test4"} in the second run.

@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();
    }
}

When I execute the test, however, a SlowCheckMemberAccess Exception is thrown. Google delivered no results for that kind of problem.

Can anyone explain what is going wrong here?

The unfiltered JUnit trace is:

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)
3
  • As always: Post the stacktrace! Commented Jun 13, 2017 at 12:53
  • 2
    Well ... "can not access a member of class org.mafagafogigante.dungeon.entity.creatures.HeroTest with modifiers private" ... Anything unclear? Commented Jun 13, 2017 at 13:10
  • 1
    Oh dear, I always had that "Filter Trace" activated in the Failure Trace few of Eclipse. That's where my confusion came from. Looking at the full trace now the mistake is obvious. Thank you for that broad hint. I edited the trace to correctly reflect the example. Commented Jun 13, 2017 at 13:36

3 Answers 3

6

The field that is annotated with @Parameter has to be public.

JUnit 4.13 will have an improved error message:

Cannot set parameter 'arguments'. Ensure that the field 'arguments' is public.
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I noticed as well looking at @Seelenvirtuose 's comment. Thanks to both of you!
0

This can happen when the class itself, not just the @Parameters method, is not declared public.

For example, if you have a test class:

public class MyTest extends AbstractTest

And then:

abstract class AbstractTest 

You'll get :

java.lang.IllegalAccessException: Class org.junit.runners.model.FrameworkMethod$1 can not access a member of class AbstractClass with modifiers "public"
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:296)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:288)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Same goes for all superclasses which might have methods called directly on them, for example if they implement an annotation like @Before.

To fix, change AbstractClass to public:

public abstract class AbstractTest 

In the JDK I'm currently using that occurs in:

sun.reflect.Reflection.verifyMemberAccess(Class<?>, Class<?>, Object, int)

Comments

-2

I do not think you need Arrays.asList() as arrays are Iterable, too.

Also Arrays.asList() can be fooled if you specify a single array argument to it. If you want to use it, add explicit cast.

1 Comment

Arrays are not iterable in a sense that you can use them as implementation of the Iterable interface. Found a discussion here: stackoverflow.com/questions/1160081/…. The notation is taken directly from the JUnit website: github.com/junit-team/junit4/wiki/Parameterized-tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.