I am trying to create some unit test but I realized I cannot mock Pattern.class
using jmock.
I get an error saying
java.lang.IllegalArgumentException: java.util.regex.Pattern is not an interface
Everytime I try to do something like this
final Pattern mockedPattern = mockery.mock(Pattern.class);
Checking on the internet I saw could be because this is a singleton bean and there is no way to mock it.
Is there any workaround to mock it? or any way to test it?
Cheers.
EDIT----
Basically, I used this answer How to disable CSRF in Spring Security 4 only for specific URL pattern through XML configuration? and it works great but I need to create the unit test for it
private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");
private RegexRequestMatcher unprotectedMatcher = new RegexRequestMatcher("/ext/**", null);
@Override
public boolean matches(HttpServletRequest request) {
if(allowedMethods.matcher(request.getMethod()).matches()){
return false;
}
return !unprotectedMatcher.matches(request);
}
IMHO, you should mock the request to test the code you posted, not Pattern
:
MockHttpServletRequest req1 = new MockHttpServletRequest("GET", "/foobar");
assertThat(theThing.matches(req1)).isFalse();
// ...