我使用groovy和junit编写单元测试。我写了一个方法testBrandIDParam
来测试一些常见的情况,如null
参数值或paramID < 0
使用反射。但是,当我测试null
参数时,此方法并不总是有效。我怎么解决这个问题?如何使用groovy中的反射调用具有null参数值的方法?
@Test
public void testGetDetailBrand() {
GetDetailReqDTO reqDTO = new GetDetailReqDTO();
testBrandIDParam(reqDTO, service, "getDetailBrand");
}
private <T> void testBrandIDParam(T requestDTO, Service service, String testMethod) {
Class requestClazz = requestDTO.getClass();
Class serviceClazz = service.getClass();
java.lang.reflect.Method doTestMethod = serviceClazz.getMethod(testMethod, requestDTO.class);
// test null
CommonRespDTO respDTO = doTestMethod.invoke(service,{null });
Assert.assertTrue(respDTO.getRespCode() == ICommonRespDTO.ResponseCode.FAIL.getCode());
T reqInstance = (T) requestClazz.newInstance();
// req-ID = 0
respDTO = (CommonRespDTO) doTestMethod.invoke(service, reqInstance)
Assert.assertTrue(!respDTO.isSuccess());
brandIDField.setAccessible(false);
}
注:getDetailBrand()
只有一个参数,brandID
。
CommonRespDTO respDTO = doTestMethod.invoke(service,{null });
抛出java.lang.IllegalArgumentException: argument type mismatch
CommonRespDTO respDTO = doTestMethod.invoke(service,new Object[1]{ null });
抛出groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.Object;.call() is applicable for argument types: (service.serviceTest$_testBrandIDParam_closure1) values: [[email protected]]
Possible solutions: tail(), wait(), any(), max(), last(), wait(long)CommonRespDTO respDTO = doTestMethod.invoke(service,new Object[1]{ null });
产生编译错误:new Objecy[] cannot be applied to groovy.lang.Closure
很难说出你在问什么,但我最好的猜测是你使用Java数组语法'{}'而不是'[]'为Groovy。 – chrylis
如何在groovy中使用null参数调用方法? –