简体   繁体   中英

why is not possible to mock Pattern.class in jmock?

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();
// ...

See MockHttpServletRequest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM