mercredi 8 juin 2016

How to invoke a method with null parameter value using reflection in groovy?

I use groovy and junit to write unit tests. I wrote a method, testBrandIDParam, to test some common cases like null parameter value or paramID < 0 using reflection. However, when I test null param, this method doesn't always work. How can I solve this problem?

@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);
}

Note: getDetailBrand() has only one argument, brandID.

  1. CommonRespDTO respDTO = doTestMethod.invoke(service,{null });
    throws

    java.lang.IllegalArgumentException: argument type mismatch

  2. CommonRespDTO respDTO = doTestMethod.invoke(service,new Object[1]{ null });
    throws

    groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.Object;.call() is applicable for argument types: (service.serviceTest$_testBrandIDParam_closure1) values: [service.serviceTest$_testBrandIDParam_closure1@28236ebc]
    Possible solutions: tail(), wait(), any(), max(), last(), wait(long)

  3. CommonRespDTO respDTO = doTestMethod.invoke(service,new Object[1]{ null });
    produces compile error:

    new Objecy[] cannot be applied to groovy.lang.Closure





Aucun commentaire:

Enregistrer un commentaire