Author Login
Post Reply
I writing a test using a custom matcher. The custom matcher seems to be working, but the error message in the failure seems off. Here is roughly what my test looks like,
ConfigurationFacet facet = context.mock(ConfigurationFacet.class)
ConfigurationService configService = context.mock(ConfigurationService.class);
context.checking(new Expectations() {{
oneOf(facet).updateResourceConfiguration(with(any(ConfigurationUpdateReport.class)));
atLeast(1).of(configService).completeConfigurationUpdate(with(matchingResponse(expectedResponse)));
}});
expectedResponse is an instance of ConfigurationUpdateResponse and matchingResponse is my custom matcher that does a property comparison. The code for it is,
public class ConfigurationUpdateResponseMatcher extends TypeSafeMatcher<ConfigurationUpdateResponse> {
@Factory
public static Matcher<ConfigurationUpdateResponse> matchingResponse(ConfigurationUpdateResponse expected) {
return new ConfigurationUpdateResponseMatcher(expected);
}
private ConfigurationUpdateResponse expected;
public ConfigurationUpdateResponseMatcher(ConfigurationUpdateResponse response) {
expected = response;
}
public boolean matchesSafely(ConfigurationUpdateResponse actual) {
return propertyEquals(expected.getConfiguration(), actual.getConfiguration()) &&
propertyEquals(expected.getConfigurationUpdateId(), actual.getConfigurationUpdateId()) &&
propertyEquals(expected.getErrorMessage(), actual.getErrorMessage()) &&
propertyEquals(expected.getStatus(), actual.getStatus());
}
private boolean propertyEquals(Object expected, Object actual) {
if (expected == null && actual == null) {
return true;
}
if (expected == null && actual != null) {
return false;
}
return expected.equals(actual);
}
public void describeTo(Description description) {
description.appendText("A " + ConfigurationUpdateResponse.class.getSimpleName() + " matching " +
expected);
}
}
I thought that the error message I would see when my custom matcher returns false would be the description it creates in its describeTo() method. Instead, I see,
un
Expected :ConfigurationUpdateResponse[configurationUpdateId=-1, ConfigurationUpdateStatus=Failure, configuration=Configuration[id=0]]
Actual :org.rhq.core.pluginapi.configuration.ConfigurationUpdateReport@50269997
)
I used values to trigger the failure as a way to verify that my custom matcher is working, but the error reporting is wrong. The object it is reporting as expected appears to be from,
oneOf(facet).updateResourceConfiguration(with(any(ConfigurationUpdateReport.class)))
but the custom matcher takes an instance of ConfigurationUpdateResponse. Is there something that I can do to alter the error message that is reported?
Thanks
- John