Home > Mobile >  junit @RunWith(Parameterized.class) annotation is throwing null
junit @RunWith(Parameterized.class) annotation is throwing null

Time:07-14

I have a test which is extending to baseTest which is where I have included the parameters.

ATest.class

public class ATest extends BaseTest {

    @Test
    public void test() {
        System.out.println(fSomething);
    }
}

BaseTest.class

@RunWith(Parameterized.class)
public class BaseTest {

    @Parameterized.Parameter
    public Boolean fSomething;

    @Parameterized.Parameters(name = "fSomething  {0}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {{true}, {false}});
    }
}

i am getting null value. If i set the constructor i am getting this error

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [boolean arg0] in constructor.

Can someone help me understand what is happening?

CodePudding user response:

In the code posted , i see @Test is being used in the derived class and using @parameterized in base class, as you are using both the annotation you see these issues. check the usage of it.

CodePudding user response:

I notice you use Jupiter from jUnit 5, but jUnit 4 API.

If you use jUnit 5, reimplement the test using @ParametrizedTest annotation instead. Start here: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests

If you want to stick with the jUnit 4 API for this particular test, replace the import for the @Test annotation: Instead of org.junit.jupiter.api.Test (jUnit 5) use org.junit.Test (jUnit 4).


For completeness, I recommend adding a constructor for the parameters in the base test class:

public BaseTest(Boolean fSomething) {
    this.fSomething = fSomething;
}

... or use Lombok annotation @AllArgsConstructor or @RequiredArgsConstructor in case you make the field final (preferred).

  • Related