【发布时间】:2020-03-08 21:13:50
【问题描述】:
class Check{
@Autowired
As400 as400; // Is a class that creates a connection to an external system
Public void execute(){
CommandCall commandCall = new CommandCall(as400); // is a class that takes the
// connection and enables us to
// execute commands on the external
//system
response = commandCall.callExternalService();
}
}
Class Checktest{
@InjectMock
Check check;
@Mock
As400 as400
@Test()
public void testExternalService(){
}
要编写一个测试,我可以模拟 As400,CommandCall 怎么样?我应该如何处理?当我在实现类中使用 new 关键字创建它时,我对如何在编写测试用例时使用模拟的 As400 感到困惑
上述的编码方式也是一种好的做法吗?还是我没有编写可测试的代码?
在编写代码时我应该注意什么,以便编写测试用例
【问题讨论】:
-
你的计划很好,只要你模拟任何对
As400方法调用的调用。但是,为什么不只是@Autowire和CommandCall?现在您并没有真正编写单元测试,您正在编写一个测试Check和CommandCall及其合同的测试。如果你需要这个来给你信心,那就太好了。
标签: spring-boot mockito testng ibm-midrange