@Test publicvoidtestGetMappingPattern() { assertEquals("/test/{param1}/path/{param2}", urlMappingPattern.getMappingPattern()); } @Test publicvoidtestCompile()throws NoSuchFieldException, IllegalAccessException { //TODO : Fix me to test the method compile(). It is better using Mockito not PowerMockito. }
@Test publicvoidtestCompile()throws NoSuchFieldException, IllegalAccessException { // Obtain compiledUrlMappingPattern field with reflection FieldcompiledUrlMappingPatternField= UrlMappingPattern.class.getDeclaredField("compiledUrlMappingPattern"); compiledUrlMappingPatternField.setAccessible(true); urlMappingPattern.compile(); // Verify that the compiledUrlMappingPattern field is updated PatterncompiledPattern= (Pattern) compiledUrlMappingPatternField.get(urlMappingPattern); assertNotNull(compiledPattern); // Verify that the mocked pattern is compiled with the expected regex Mockito.verify(urlMappingPattern.compiledUrlMappingPattern) .compile("/test/([%\\w-.\\~!$&'\\(\\)\\*\\+,;=:\\[\\]@]+?)/path/([%\\w-.\\~!$&'\\(\\)\\*\\+,;=:\\[\\]@]+?)(?:\\?.*?)?$"); }
但是跑测试类时,会使别的测试方法报错(虽然报错信息依然指向这里):
1 2 3 4 5 6 7 8 9 10 11 12 13
org.mockito.exceptions.misusing.UnfinishedVerificationException: Missing method call for verify(mock) here: -> at org.apache.eventmesh.admin.rocketmq.util.UrlMappingPatternTest.testCompile(UrlMappingPatternTest.java:95)
Example of correct verification: verify(mock).doSomething()
Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods. Those methods *cannot* be stubbed/verified. Mocking methods declared on non-public parent classes is not supported.
at org.apache.eventmesh.admin.rocketmq.util.UrlMappingPatternTest$TestUrlMappingPattern.<init>(UrlMappingPatternTest.java:105) at org.apache.eventmesh.admin.rocketmq.util.UrlMappingPatternTest.setUp(UrlMappingPatternTest.java:44)
@Test publicvoidtestCompile()throws NoSuchFieldException, IllegalAccessException { // Obtain compiledUrlMappingPattern field with reflection FieldcompiledUrlMappingPatternField= UrlMappingPattern.class.getDeclaredField("compiledUrlMappingPattern"); compiledUrlMappingPatternField.setAccessible(true);
urlMappingPattern.compile();
// Verify that the compiledUrlMappingPattern field is updated PatterncompiledPattern= (Pattern) compiledUrlMappingPatternField.get(urlMappingPattern); assertNotNull(compiledPattern);
// Verify that the mocked pattern is compiled with the expected regex StringexpectedRegex="/test/([%\\w-.\\~!$&'\\(\\)\\*\\+,;=:\\[\\]@]+?)/path/([%\\w-.\\~!$&'\\(\\)\\*\\+,;=:\\[\\]@]+?)(?:\\?.*?)?$"; PatternexpectedPattern= Pattern.compile(expectedRegex); assertEquals(expectedPattern.pattern(), compiledPattern.pattern()); }