当存根方法时接收InvalidUseOfMatchersException

时间:2014-05-21 21:42:22

标签: java exception mocking mockito

我目前与Mockito有问题。但首先,我的代码创建了Mockito抱怨​​的模拟:

    @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和... Visitor类来自asm(所以如果你想测试这个设置,你只需要导入asm,junit和mockito库)。当我调用createMockedClassWriter()时,抛出以下异常

when(classWriter.visitAnnotation(anyString(), anyBoolean())).thenReturn(annotationVisitor); 

被称为:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 

参数匹配器的使用无效! 1匹配预期,2记录: - &GT; at com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.createMockedClassWriter(RestEndpointByteCodeGeneratorTest.java:91) - &GT; at com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.createMockedClassWriter(RestEndpointByteCodeGeneratorTest.java:91)

如果匹配器与原始值组合,则可能会发生此异常:     //不正确:     someMethod(anyObject(),&#34; raw String&#34;); 使用匹配器时,所有参数都必须由匹配器提供。 例如:     //正确:     someMethod(anyObject(),eq(&#34; matcher by matcher&#34;));

有关更多信息,请参阅Matchers类的javadoc。

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)

正如您所看到的,我尝试过不同风格的存根创建,但这似乎不是问题。

有人可以帮助我吗?

问候, 帕斯卡

1 个答案:

答案 0 :(得分:2)

visitAnnotation是最终方法,thus cannot be mocked

public final AnnotationVisitor visitAnnotation(String desc, boolean visible)

有关幕后内容的更多信息以及您在该行上获得该例外的原因,请参阅“How do Mockito matchers work?”。