缺少嵌套异常

时间:2017-05-05 15:48:31

标签: java exception junit

我正在从junit类中对我的无状态会话bean进行一些测试,所以流程就像这样。

Junit测试方法调用bean A中的业务方法。此方法抛出CustomException。

Bean业务方法调用bean B业务方法。此方法还会抛出CustomException。

Bean B调用一个抛出BeanCException的远程Bean C. Bean B捕获AccountBadFormatException,将其包装在CustomException中,并且throws返回Bean A.

从Bean返回一个CustomException回到JUnit调用代码。当我在JUnit中捕获CustomException并查看它时,里面没有AccountBadFormatException。原因是" CustomException"并且堆栈跟踪已经消失。但是如果我在它被抛到JUnit之前在Bean A中查看它,嵌套异常就在那里。为什么会这样?

JUnit类:

@Test
public void testGetResult() {

    setupContext();

    Result result = null;
    try {
        result = (Result) serviceInterface.getResult("000001", "01011970");
    } catch (CustomException e) {
        System.out.println("CustomException getCause() in JUnit: " + e.getCause().toString());
        fail();
    }

    assertTrue((result.getReturnCode() == ReturnCode.OK));
}

Bean A:

   @Override
public Result getResult(String clientID, String dob) throws CustomException {

    Result result = new Result(ReturnCode.ERROR);
    boolean isMatched = false;
    try {
        isMatched = Lookup.getUtilityService().isIdentMatched(clientID, dob);
    } catch (CustomException e) {
        System.out.println("CustomException getCause() in Bean A: " + e.getCause().toString());
        throw e;
    }

    if (isMatched) {
        result.setReturnCode(ReturnCode.OK);
    }

    return result;

}

Bean B:

    @Override
public boolean isIdentMatched(String clientID, String dob) throws CustomException {



    IdentRequest request = new IdentRequest(clientID);
    ClientSummary response = null;
    // retrieve the data
    try {
        response = getRetriever().getClientSummaryView(request)
                .getClientSummary();
    } catch (XrefAccountNumberException e) {
        throw new CustomException(e.getMessage(), e);
    } catch (AccountNotFoundException e) {
        throw new CustomException(e.getMessage(), e);
    } catch (AccountBadFormatException e) {
        throw new CustomException(e.getMessage(), e);
    } catch (ExclusiveLockException e) {
        throw new CustomException(e.getMessage(), e);
    } catch (RemoteException e) {
        throw new CustomException(e.getMessage(), e);
    } catch (ValidationException e) {
        throw new CustomException(e.getMessage(), e);
    }

    return response.isMismatch();
}

Bean A中print语句的输出是:" Bean A中的CustomException getCause():ca.gc.cra.iica.iasi.common.exceptions.AccountBadFormatException"

JUnit中的print语句是:" JUnit中的CustomException getCause():ca.gc.cra.fltr.filemyreturn.business.exceptions.CustomException"

0 个答案:

没有答案