Last active
July 4, 2018 17:43
-
-
Save ToastShaman/9052565 to your computer and use it in GitHub Desktop.
Nested Hamcrest Matcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void shouldCheckWhetherWeHaveAnUsernameAlreadyInUseErrorCode() { | |
ServiceException e = new ServiceException("ERR500|532323"); | |
assertThat(e, hasErrorCode(usernameAlreadyInUse())); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hamcrest.Description; | |
import org.hamcrest.Factory; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
public class ServiceExceptionMatcher extends TypeSafeMatcher<ServiceException> { | |
private Matcher<?> matcher; | |
public ServiceExceptionMatcher(Matcher<?> matcher) { | |
this.matcher = matcher; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendDescriptionOf(matcher); | |
} | |
@Override | |
public boolean matchesSafely(ServiceException item) { | |
return matcher.matches(item); | |
} | |
@Factory | |
public static <T> Matcher<ServiceException> hasErrorCode(Matcher<?> matcher) { | |
return new ServiceExceptionMatcher(matcher); | |
} | |
public static Matcher<?> usernameAlreadyInUse() { | |
return org.hamcrest.Matchers.hasProperty("errorCode", org.hamcrest.core.IsEqual.equalTo("ERR500|532121")); | |
} | |
public static Matcher<?> invalidCredentials() { | |
return org.hamcrest.Matchers.hasProperty("errorCode", org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase("ERR500|532126")); | |
} | |
public static Matcher<?> ldapAccessErrorInvalidCredentials() { | |
return org.hamcrest.Matchers.hasProperty("errorCode", org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase("ERR500|532343")); | |
} | |
public static Matcher<?> ldapAccessErrorInsufficientCredentials() { | |
return org.hamcrest.Matchers.hasProperty("errorCode", org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase("ERR500|532323")); | |
} | |
public static Matcher<?> ldapAccessErrorInvalidUsernameOrPassword() { | |
return org.hamcrest.Matchers.hasProperty("errorCode", org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase("ERR500|5321230")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment