在使用使用新关键字创建的对象上使用模仿

问题描述 投票:0回答:1
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; //是一个创建与外部系统的连接的类//是一个接受...

spring-boot mockito testng ibm-midrange
1个回答
0
投票

是,您还应该使CommandCall类可注入,以便轻松地对代码进行单元测试。您可以创建一个新的配置类,然后在其中将CommandCall定义为bean,然后将其注入Check

© www.soinside.com 2019 - 2024. All rights reserved.