我目前有一个问题与Mockito。但首先这是我的代码,它创建了Mockito抱怨的mock:
@Test
public void testRestEndpointGeneration() {
//Create a mocked classWriter to control the classWriter calls
ClassWriter classWriter = createMockedClassWriter();
//Create the list of annotated method parameters which should be used to generate the class
List<AnnotatedMethodParameter> methodParameters = new ArrayList<>();
methodParameters.add(new AnnotatedMethodParameter("testContext", "Ljavax/ws/rs/core/SecurityContext;", "Ljavax/ws/rs/core/SecurityContext;", ParameterType.CONTEXT));
methodParameters.add(new AnnotatedMethodParameter("testMatrix", "Ljava/lang/Integer;", "Ljava/lang/Integer;", ParameterType.MATRIXPARAM));
methodParameters.add(new AnnotatedMethodParameter("testForm", "Ljava/lang/Double;", "Ljava/lang/Double;", ParameterType.FORMPARAM));
RestEndpointByteCodeGenerator.createClassByteCode("EndpointGenerationTest",
"test/{testPath}",
HttpMethod.GET,
new String[]{"application/json", "application/xml"},
new String[]{"application/json", "application/xml"},
methodParameters,
new MethodParameter("Ljava/util/Map;", "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;"),
classWriter);
AnnotationVisitor annotationVisitor = classWriter.visitAnnotation("", true);
FieldVisitor fieldVisitor = classWriter.visitField(1, "", "", null, null);
MethodVisitor methodVisitor = classWriter.visitMethod(1, null, null, null, null);
//Now check that the methods on the classWriter were correctly invoked
InOrder inOrder = inOrder(classWriter);
inOrder.verify(classWriter).visit(V1_7, ACC_PUBLIC + ACC_SUPER, "EndpointGenerationTest", null, "com/sportslivefinder/service/rest/generatedEndpoints/RestEndpointParent", null);
inOrder.verify(classWriter).visitAnnotation("Ljavax/ws/rs/Path;", true);
inOrder.verify(classWriter).visitField(ACC_PRIVATE + ACC_FINAL, "processor", "Lorg/apache/camel/Processor;", null, null);
inOrder.verify(classWriter).visitMethod(ACC_PUBLIC, "processRequest", "(Ljavax/ws/rs/core/SecurityContext;Ljava/lang/Integer;Ljava/lang/Double;Ljava/util/Map;)Ljavax/ws/rs/core/Response;",
"(Ljavax/ws/rs/core/SecurityContext;Ljava/lang/Integer;Ljava/lang/Double;Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)Ljavax/ws/rs/core/Response;", null);
inOrder = inOrder(annotationVisitor);
inOrder.verify(annotationVisitor).visit("value", "test/{testPath}");
inOrder.verify(annotationVisitor).visitEnd();
inOrder.verify(annotationVisitor).visitEnd();
for(int i = 0; i < 1; i++){
inOrder.verify(annotationVisitor).visitArray("value");
inOrder.verify(annotationVisitor).visit(null, "application/json");
inOrder.verify(annotationVisitor).visit(null, "application/xml");
inOrder.verify(annotationVisitor).visitEnd();
inOrder.verify(annotationVisitor).visitEnd();
}
inOrder.verify(annotationVisitor).visit("value", "/");
inOrder.verify(annotationVisitor).visitEnd();
inOrder.verify(annotationVisitor).visitEnd();
inOrder.verify(annotationVisitor).visit("value", "testMatrix");
inOrder.verify(annotationVisitor).visitEnd();
inOrder.verify(annotationVisitor).visit("value", "testForm");
inOrder.verify(annotationVisitor).visitEnd();
inOrder = inOrder(fieldVisitor);
inOrder.verify(fieldVisitor).visitEnd();
inOrder = inOrder(methodVisitor);
inOrder.verify(methodVisitor).visitAnnotation("Ljavax/ws/rs/GET;", true);
inOrder.verify(methodVisitor).visitAnnotation("Ljavax/ws/rs/Produces;", true);
inOrder.verify(methodVisitor).visitAnnotation("Ljavax/ws/rs/Consumes;", true);
inOrder.verify(methodVisitor).visitAnnotation("Ljavax/ws/rs/Path;", true);
}
private ClassWriter createMockedClassWriter(){
ClassWriter classWriter = mock(ClassWriter.class);
final AnnotationVisitor annotationVisitor = createMockedAnnotationVisitor();
final MethodVisitor methodVisitor = createMockedMethodVisitor(annotationVisitor);
final FieldVisitor fieldVisitor = mock(FieldVisitor.class);
when(classWriter.visitAnnotation(anyString(), anyBoolean())).thenReturn(annotationVisitor);
doReturn(annotationVisitor).when(classWriter).visitAnnotation(anyString(), anyBoolean());
doReturn(fieldVisitor).when(classWriter).visitField(anyInt(), anyString(), anyString(), anyString(), any());
doReturn(methodVisitor).when(classWriter).visitMethod(anyInt(), anyString(), anyString(), anyString(), any(String[].class));
return classWriter;
}
private AnnotationVisitor createMockedAnnotationVisitor(){
final AnnotationVisitor visitor = mock(AnnotationVisitor.class);
doReturn(visitor).when(visitor).visitArray(anyString());
return visitor;
}
private MethodVisitor createMockedMethodVisitor(AnnotationVisitor annotationVisitor){
MethodVisitor methodVisitor = mock(MethodVisitor.class);
doReturn(annotationVisitor).when(methodVisitor).visitAnnotation(anyString(), anyBoolean());
return methodVisitor;
}
ClassWriter and The…访问者类来自asm(所以如果你想测试这个设置,你只需要导入asm、junit和mockito库)。当我调用createMockedClassWriter()时,当
when(classWriter.visitAnnotation(anyString(), anyBoolean())).thenReturn(annotationVisitor);
被称为:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
参数匹配器的无效使用!预期1个匹配器,记录2个;-> at com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.createMockedClassWriter(RestEndpointByteCodeGeneratorTest.java:91)-> at com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.createMockedClassWriter(RestEndpointByteCodeGeneratorTest.java:91)
如果匹配器与原始值结合使用,可能会出现此异常://不正确的:someemethod (anyObject(), "raw String");当使用匹配器时,所有参数都必须由匹配器提供。例如://正确的:someemethod (anyObject(), eq("String by matcher"));
更多信息请参见javadoc For Matchers类。
at org.objectweb.asm.ClassWriter.visitAnnotation(Unknown Source)
at com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.createMockedClassWriter(RestEndpointByteCodeGeneratorTest.java:91)
at com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.testRestEndpointGeneration(RestEndpointByteCodeGeneratorTest.java:28)
正如你所看到的,我已经尝试了不同风格的存根创建,但这似乎不是问题。
有谁能帮我一下吗?问候,帕斯卡
visitAnnotation
是final方法,因此不能被mock。
public final AnnotationVisitor visitAnnotation(String desc, boolean visible)
有关幕后发生的事情的更多信息,以及为什么会在该行上出现异常,请参阅"How do Mockito匹配器工作?