gpt4 book ai didi

java - JUnit 测试未捕获异常

转载 作者:行者123 更新时间:2023-12-01 19:30:18 24 4
gpt4 key购买 nike

更新:这是完整的测试:

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

这是构建方法:

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 代码是错误的。我错过了什么明显的事情吗?感谢您提供任何有用的提示。

最佳答案

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

参见

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) 仅处理被测试代码或测试本身未处理的异常。

关于java - JUnit 测试未捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59949014/

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