gpt4 book ai didi

java - stub 方法时收到 InvalidUseOfMatchersException

转载 作者:行者123 更新时间:2023-12-02 05:48:31 28 4
gpt4 key购买 nike

我目前在使用 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 名:-> 在 com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.createMockedClassWriter(RestEndpointByteCodeGeneratorTest.java:91)-> 在 com.sportslivefinder.service.rest.classCreation.RestEndpointByteCodeGeneratorTest.createMockedClassWriter(RestEndpointByteCodeGeneratorTest.java:91)

如果匹配器与原始值组合,则可能会发生此异常: //不正确: someMethod(anyObject(), "原始字符串");使用匹配器时,所有参数都必须由匹配器提供。例如: //正确的: someMethod(anyObject(), eq("匹配器的字符串"));

有关详细信息,请参阅 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)

如您所见,我尝试了不同样式的 stub 创建,但这似乎不是问题。

有人可以帮我吗?

您好,帕斯卡

最佳答案

visitAnnotation是 final方法,并且 thus cannot be mocked .

public final AnnotationVisitor visitAnnotation(String desc, boolean visible)

有关幕后情况以及为何在该行出现异常的更多信息,请参阅“How do Mockito matchers work? ”。

关于java - stub 方法时收到 InvalidUseOfMatchersException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23794324/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com