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感到困惑。
而且上述编码方法也是一种好的做法吗?还是我不编写可测试的代码?
并且在编写代码时应该注意什么,以便于编写测试用例
class Check {@Autowired As400 as400; //是一个创建与外部系统的连接的类//是一个接受...
是,您还应该使CommandCall
类可注入,以便轻松地对代码进行单元测试。您可以创建一个新的配置类,然后在其中将CommandCall
定义为bean,然后将其注入Check
类