我有一个扩展到baseTest的测试,它是我包含参数的地方。
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}});
}
}我得到的是空值。如果设置构造函数,就会得到这个错误。
org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [boolean arg0] in constructor.有人能帮我了解一下发生了什么吗?
发布于 2022-07-14 03:59:06
我注意到您使用jUnit 5中的木星,但是jUnit 4 API。
如果使用jUnit 5,则使用@ParametrizedTest注释重新实现测试。从这里开始:https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests
如果您想在这个特定的测试中坚持使用jUnit 4 API,请替换@Test注释的导入:而不是org.junit.jupiter.api.Test (jUnit 5)使用org.junit.Test (jUnit 4)。
为了完整起见,我建议为基类中的参数添加一个构造函数:
public BaseTest(Boolean fSomething) {
this.fSomething = fSomething;
}..。或者使用Lombok注释@AllArgsConstructor或@RequiredArgsConstructor,以防您将字段设置为final (首选)。
发布于 2022-07-14 03:55:19
在发布的代码中,我看到在派生类中使用@Test,在基类中使用@参数化,因为在使用两个注释时都会看到这些问题。检查它的用法。
https://stackoverflow.com/questions/72974126
复制相似问题