为什么不能在 jmock 中模拟 Pattern.class?

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

我正在尝试创建一些单元测试,但我意识到我无法使用 jmock 模拟 Pattern.class

我收到一条错误消息

java.lang.IllegalArgumentException: java.util.regex.Pattern is not an interface

每次我尝试做这样的事情时

final Pattern mockedPattern = mockery.mock(Pattern.class);

上网查了一下,可能是因为这是一个单例bean,没有办法mock。

是否有任何解决方法来模拟它?或者有什么方法可以测试吗?

干杯。

编辑----

基本上,我使用了这个答案 并且效果很好,但我需要为其创建单元测试

 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);
    }

恕我直言,您应该模拟请求来测试您发布的代码,而不是 Pattern:

MockHttpServletRequest req1 = new MockHttpServletRequest("GET", "/foobar");
assertThat(theThing.matches(req1)).isFalse();
// ...

MockHttpServletRequest