gpt4 book ai didi

java - Mockito/Spring MVC -(注解驱动)请求映射动态验证

转载 作者:行者123 更新时间:2023-12-04 05:56:01 25 4
gpt4 key购买 nike

我目前正在编写一个基于 Spring MVC 的 webapp。

我不想为每个带注释的方法编写一个测试,而是希望从 Parameterized JUnit runner 中受益。

最后,我几乎可以正常工作,尽管我必须在我的 Controller 方法中将所有原始参数更改为它们的包装对应物(然后手动对空引用进行完整性检查)。

如果它可以帮助,这里是代码(这也取决于 Guava ):

@RunWith(Parameterized.class)
public class MyControllerMappingTest {

private MockHttpServletRequest request;
private MockHttpServletResponse response;
private MyController mockedController;
private AnnotationMethodHandlerAdapter annotationHandlerAdapter;
private final String httpMethod;
private final String uri;
private final String controllerMethod;
private final Class<?>[] parameterTypes;
private final Object[] parameterValues;

@Before
public void setup() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
mockedController = mock(MyController.class);
annotationHandlerAdapter = new AnnotationMethodHandlerAdapter();
}

@Parameters
public static Collection<Object[]> requestMappings() {
return asList(new Object[][] {
{"GET", "/my/uri/0", "index", arguments(new MethodArgument(Integer.class, 0))}
});
}

private static List<MethodArgument> arguments(MethodArgument... arguments) {
return asList(arguments);
}

public MyControllerMappingTest(String httpMethod, String uri, String controllerMethod, List<MethodArgument> additionalParameters) {
this.httpMethod = httpMethod;
this.uri = uri;
this.controllerMethod = controllerMethod;
this.parameterTypes = new Class<?>[additionalParameters.size()];
initializeParameterTypes(additionalParameters);
this.parameterValues = newArrayList(transform(additionalParameters, valueExtractor())).toArray();
}

private void initializeParameterTypes(List<MethodArgument> additionalParameters) {
Iterable<Class<?>> classes = transform(additionalParameters, typeExtractor());
int i = 0;
for (Class<?> parameterClass : classes) {
parameterTypes[i++] = parameterClass;
}
}

@Test
public void when_matching_mapping_constraints_then_controller_method_automatically_called() throws Exception {
request.setMethod(httpMethod);
request.setRequestURI(uri);
annotationHandlerAdapter.handle(request, response, mockedController);

Method method = MyController.class.getMethod(controllerMethod, parameterTypes);
method.invoke(verify(mockedController), parameterValues);
}
}

使用以下自定义类 MethodArgument:
public class MethodArgument {
private final Class<?> type;
private final Object value;

public MethodArgument(final Class<?> type, final Object value) {
this.type = type;
this.value = value;
}

public Object getValue() {
return value;
}

public Class<?> getType() {
return type;
}

public static Function<MethodArgument, Class<?>> typeExtractor() {
return new Function<MethodArgument, Class<?>>() {
@Override
public Class<?> apply(MethodArgument argument) {
return argument.getType();
}
};
}

public static Function<MethodArgument, Object> valueExtractor() {
return new Function<MethodArgument, Object>() {
@Override
public Object apply(MethodArgument argument) {
return argument.getValue();
}
};
}
}

所以,我快到了,这里唯一的测试用例因为 Java Integer 缓存而有效,因此 Integer 实例在整个调用链中都是相同的......但是这不适用于自定义对象,我总是以一个 InvocationTargetException (原因:“参数不同!”)...

类型是正确的,但传递的实例与 @Parameters 方法中设置的实例不同。

知道如何解决这个问题吗?

最佳答案

稍安毋躁!

SpringSource 正在烘焙一个 spring-test-mvc 模块:
https://github.com/SpringSource/spring-test-mvc

关于java - Mockito/Spring MVC -(注解驱动)请求映射动态验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507601/

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