I want to provide an API to an API consumer. This API can be used to perform certain tests easily. I'm using AssertJ custom assertions to build this API. While the API consumer calls or uses my custom assertions, I would like him to easily configure use of soft assertions or hard assertions. Based on this cofiguration, internally I would like to make a choice between
Either Using
Assertions.assertThat(...)
Or Using
softlyAssertInstance.assertThat(...)
I don't want to put a check at all places that looks like follows:
if(softlyAssertInstance != null) {
softlyAssertInstance.assertThat(...);
} else {
Assertions.assertThat(...);
}
Rather I would prefer something like follows:
getAssertor().assertThat(...);
This way I can do my if-else check within getAssertor like follows:
public ReturnType getAssertor() {//Don't know what my return type should be here
if(softlyAssertInstance != null)
return softlyAssertInstance;
else
return Assertions;
}
I was thinking about this but a return type Object doesn't quite help as I narrow down the methods that can be called on returned object. For instance
Object assertor = getAssertor();
assertor.assertThat();//this isn't possible unless I cast it down
I would like to have this happen dynamically. Only way I can see forward is via Java Reflections where I can probably return instance of Method. This may look like follows:
public Method getAssertingMethod() {
if(softlyAssertInstance != null) {
return softlyAssertInstance.class.getDeclaredMethod("assertThat", {String.class});
else
return Assertions.class.getDeclaredMethod("assertThat", {String.class});
}
I would still need to dynamically identify whether to use String.class or Boolean.class here based on calling code. This seems to be a performence intensive route which I feel is just too complicated. Any suggestions that would help me achieve my requirement in a better way?