Java 使用AssertJ在软断言和硬断言之间动态切换
我想向API使用者提供API。此API可用于轻松执行某些测试。我正在使用AssertJ自定义断言来构建这个API。 当API使用者调用或使用我的自定义断言时,我希望他能够轻松配置软断言或硬断言的使用。基于此配置,我想在内部选择 或者使用Java 使用AssertJ在软断言和硬断言之间动态切换,java,inheritance,assertj,Java,Inheritance,Assertj,我想向API使用者提供API。此API可用于轻松执行某些测试。我正在使用AssertJ自定义断言来构建这个API。 当API使用者调用或使用我的自定义断言时,我希望他能够轻松配置软断言或硬断言的使用。基于此配置,我想在内部选择 或者使用 Assertions.assertThat(...) 或使用 softlyAssertInstance.assertThat(...) 我不想在所有地方都进行检查,如下所示: if(softlyAssertInstance != null) { so
Assertions.assertThat(...)
或使用
softlyAssertInstance.assertThat(...)
我不想在所有地方都进行检查,如下所示:
if(softlyAssertInstance != null) {
softlyAssertInstance.assertThat(...);
} else {
Assertions.assertThat(...);
}
getAssertor().assertThat(...);
public ReturnType getAssertor() {//Don't know what my return type should be here
if(softlyAssertInstance != null)
return softlyAssertInstance;
else
return Assertions;
}
public Method getAssertingMethod() {
if(softlyAssertInstance != null) {
return softlyAssertInstance.class.getDeclaredMethod("assertThat", {String.class});
else
return Assertions.class.getDeclaredMethod("assertThat", {String.class});
}
相反,我更喜欢以下内容:
if(softlyAssertInstance != null) {
softlyAssertInstance.assertThat(...);
} else {
Assertions.assertThat(...);
}
getAssertor().assertThat(...);
public ReturnType getAssertor() {//Don't know what my return type should be here
if(softlyAssertInstance != null)
return softlyAssertInstance;
else
return Assertions;
}
public Method getAssertingMethod() {
if(softlyAssertInstance != null) {
return softlyAssertInstance.class.getDeclaredMethod("assertThat", {String.class});
else
return Assertions.class.getDeclaredMethod("assertThat", {String.class});
}
这样,我可以在getAssertor中执行if-else检查,如下所示:
if(softlyAssertInstance != null) {
softlyAssertInstance.assertThat(...);
} else {
Assertions.assertThat(...);
}
getAssertor().assertThat(...);
public ReturnType getAssertor() {//Don't know what my return type should be here
if(softlyAssertInstance != null)
return softlyAssertInstance;
else
return Assertions;
}
public Method getAssertingMethod() {
if(softlyAssertInstance != null) {
return softlyAssertInstance.class.getDeclaredMethod("assertThat", {String.class});
else
return Assertions.class.getDeclaredMethod("assertThat", {String.class});
}
我曾考虑过这个问题,但返回类型对象并没有太大帮助,因为我缩小了可以对返回对象调用的方法的范围。比如说
Object assertor = getAssertor();
assertor.assertThat();//this isn't possible unless I cast it down
我希望这种情况动态发生。我向前看的唯一方法是通过Java反射,在那个里我可能可以返回方法的实例。这可能如下所示:
if(softlyAssertInstance != null) {
softlyAssertInstance.assertThat(...);
} else {
Assertions.assertThat(...);
}
getAssertor().assertThat(...);
public ReturnType getAssertor() {//Don't know what my return type should be here
if(softlyAssertInstance != null)
return softlyAssertInstance;
else
return Assertions;
}
public Method getAssertingMethod() {
if(softlyAssertInstance != null) {
return softlyAssertInstance.class.getDeclaredMethod("assertThat", {String.class});
else
return Assertions.class.getDeclaredMethod("assertThat", {String.class});
}
我仍然需要根据调用代码动态确定是使用String.class还是Boolean.class。这似乎是一条表演密集型路线,我觉得太复杂了。有什么建议可以帮助我更好地实现我的需求吗?为什么您希望消费者“轻松配置软断言或硬断言的使用”?