【发布时间】:2021-10-17 13:14:32
【问题描述】:
我正在尝试为以下类编写测试,其中映射字段通过构造函数注入注入依赖项的参数列表。如何模拟依赖项?
@Component
public class ComponentInputValidator {
private final Map<String, FaceInterface> faceMap;
private final Map<String, ArmsInterface> armsMap;
private final Map<String, MobilityInterface> mobilityMap;
private final Map<String, MaterialInterface> materialMap;
private final RobotComponentStock robotComponentStock;
public ComponentInputValidator(List<MaterialInterface> materialList,
List<FaceInterface> faceList,
List<ArmsInterface> armsList,
List<MobilityInterface> mobilityList,
RobotComponentStock robotComponentStock){
this.faceMap = faceList.stream().collect(Collectors.toMap(faceInterface -> faceInterface.getCode().name(), Function.identity()));
this.armsMap = armsList.stream().collect(Collectors.toMap(armsInterface -> armsInterface.getCode().name(), Function.identity()));
this.mobilityMap = mobilityList.stream().collect(Collectors.toMap(mobilityInterface -> mobilityInterface.getCode().name(), Function.identity()));
this.materialMap = materialList.stream().collect(Collectors.toMap(materialInterface -> materialInterface.getCode().name(), Function.identity()));
this.robotComponentStock = robotComponentStock;
}
public boolean validateStockAvailability(RobotComponent robotComponent){
String face = robotComponent.getFace();
String arms = robotComponent.getArms();
String mobility = robotComponent.getMobility();
String material = robotComponent.getMaterial();
Code faceCode = faceMap.get(face).getCode();
Code armsCode = armsMap.get(arms).getCode();
Code mobilityCode = mobilityMap.get(mobility).getCode();
Code materialCode = materialMap.get(material).getCode();
if (robotComponentStock.getQuantity(faceCode)<1 ...{
...
return false;
}
return true;
}
}
FaceInterface、ArmsInterface、MobilityInterface、MaterialInterface 是具有不同实现的接口。
我尝试了什么:
@MockBean
private RobotComponentStock robotComponentStock;
@MockBean
private List<FaceInterface> faceInterfaceList;
@MockBean
private List<MobilityInterface> mobilityInterfaceList;
@MockBean
private List<ArmsInterface> armsInterfaceList;
@MockBean
private List<MaterialInterface> materialInterfaceList;
@InjectMocks
private ComponentInputValidator componentInputValidator;
出现错误: org.mockito.exceptions.misusing.InjectMocksException: 无法实例化名为“com.demo.robot_factory.service.ComponentInputValidator”类型的名为“componentInputValidator”的@InjectMocks 字段。 您没有在字段声明时提供实例,所以我尝试构建实例。 但是构造函数或初始化块抛出异常:null 为线
faceList.stream().collect(Collectors.toMap(faceInterface -> faceInterface.getCode().name(), Function.identity()));
【问题讨论】:
-
“如何”是什么意思?你用 Mockito 标记了你的问题,那么明显吗?
-
我不认为我们需要测试bean ComponentInputValidator,如果我们把它当作普通类并用它编写单元测试会更好。然后我们可以传递不同的参数,看看构造函数是否按预期工作。
-
不知道怎么写mock?你能告诉我怎么做吗?我尝试了几种方法,但收到了空指针异常。我知道我做错了什么,但无法弄清楚。我被这个困住了。
-
你能发布一些你尝试过的方法吗?你到底是从哪里获得 NPE 的?
-
@ParthManaktala 我想做的是这样的: given(robotComponentStock.getQuantity(faceCode)).willReturn(0); assertThat(componentInputValidator.validateStockAvailability(robotComponent)).isFalse();如何编写 MockBean 和 InjectMocks ?
标签: java spring mockito junit5