JUnit测试未捕获异常

时间:2020-01-28 12:41:52

标签: java junit

更新:这是完整的测试:

@Test(expected = NullPointerException.class)
public void testMissingData() throws Exception{
Resource<ObjectDataModel, Content, Status> resource = builder.build(content,  argA1Response, 
            argA2Response, objFilterParam, argA3Response);}

这是 build 方法:

public Resource<ObjectDataModel, Content, Status> build(Content argContent,
        ResponseA1 argA1Response,
        ResponseA2 argA2Response, String argObjectTypeFilter,
        ResponseA3 argA3Response) {

    try {
        viewDataModel.setObjectType(this.buildObjectType(filteredObjectType,
                argA1Response.getData().getDataObject().getCategories().get(0).getObjectTypes().get(0)));

    }
    catch (Exception e) {
        String msg = "Exception occoured while buildng the Object Data Model";
        LOG.error(msg, e);
    }

    // we have the required information gathered to return
    return Resource.okFromDataAndContent(viewDataModel, argContent);
}

这是 buildObjectType()方法:

private ObjectType buildObjectType(ObjectTypes argA1ProductType,
        PendingObjectTypes argA2ProductType) {
    ProductType objectType = new ObjectType();
    List<Plan> plans = argA1ObjectType.getPlan();
    List<PendingObjectSummary> objPlans = argA1ObjectType.getData();

    if (objectType.getData() == null) {
        objectType.setData(new ArrayList<>());
    }

    PendingObjectSummary tempPlan = null;
    for (Plan currPlan : plans) {
        tempPlan = plans.stream()
                        .filter(plan -> plan.getObjId().equals(currPlan.getObjId()))
                        .findFirst()
                        .orElseThrow(NullPointerException::new);

    }

    return objectType;
}

我正在使用 Optional 来测试是否为null,并且可以确认引发了异常-但JUnit并未捕获到该异常。这是测试用例:

@Test(expected = NullPointerException.class)
public void testMissingData() throws Exception{
    Object<> response = fixture.create();

    assertNotNull(response);
    assertNotNull(response.getData());

    assertNull(resource.getData().getObjectType()); 
}

在我的 create 方法中,我只是遍历一堆对象以尝试找到与我的ID匹配的对象。如果找不到,则抛出 NullPointerException

for (Object currObj : objects) {
        tempObj = myOtherCollection.stream()
                        .filter(obj -> obj.getId().equals(currObj.getId()))
                        .findFirst()
                        .orElseThrow(NullPointerException::new);
    }

JUnit输出显然没有捕获异常-这是输出:

java.lang.AssertionError: Expected exception: java.lang.NullPointerException

我的tomcat日志肯定在这里抛出异常:

18:48:30.015 [main] ERROR com.myCompany.src.ModelBuilder - Exception occoured while buildng the Data Model
java.lang.NullPointerException: null
at java.util.Optional.orElseThrow(Optional.java:290)

我唯一看到的问题是,也许我在分配 tempObj 的地方输入了错误的代码。我有什么明显的遗漏吗?感谢您提供任何有用的提示。

2 个答案:

答案 0 :(得分:3)

您正在捕获nullpointer异常,因此该异常不会传播到您的测试中。

请参见

try {
    viewDataModel.setObjectType(this.buildObjectType(filteredObjectType,
            argA1Response.getData().getDataObject().getCategories().get(0).getObjectTypes().get(0)));

}
catch (Exception e) {
    String msg = "Exception occoured while buildng the Object Data Model";
    LOG.error(msg, e);
}

如果要测试异常,可以在错误处理中抛出异常(例如,自定义ObjectCreationExcepion),并断言已抛出异常,例如

try {
    viewDataModel.setObjectType(this.buildObjectType(filteredObjectType,
            argA1Response.getData().getDataObject().getCategories().get(0).getObjectTypes().get(0)));

}
catch (Exception e) {
    String msg = "Exception occoured while buildng the Object Data Model";
    LOG.error(msg, e);
    throw new ObjectCreationException(msg);
}

并在测试中

@Test(expected = ObjectCreationException.class) 
public void testMissingData() throws Exception{
    Object<> response = fixture.create();  
}

@Test(expected = ObjectCreationException.class)仅处理在测试代码或测试本身中未处理的异常。

答案 1 :(得分:0)

所以你可以做的是

public Resource<ObjectDataModel, Content, Status> build(Content argContent,
        ResponseA1 argA1Response,
        ResponseA2 argA2Response, String argObjectTypeFilter,
        ResponseA3 argA3Response) throws NullPointerExceptions // << notice thrwoing declatration
{ // do some stuf} 

然后在测试中,您可以像尝试

一样处理它
public void testMissingData() throws Exception{
Resource<ObjectDataModel, Content, Status> resource = builder.build(content,  argA1Response, 
            argA2Response, objFilterParam, argA3Response);
}