Java 在junit中模拟方法中的变量
如果我正在为以下方法编写Junit,如何模拟clientInstance.executeJava 在junit中模拟方法中的变量,java,junit,mockito,Java,Junit,Mockito,如果我正在为以下方法编写Junit,如何模拟clientInstance.execute public class ClassName{ public static <type> fun(){ HttpClient clientInstance = null; // clientInstance initialised here response = clientInstance.execute(); //I
public class ClassName{
public static <type> fun(){
HttpClient clientInstance = null;
// clientInstance initialised here
response = clientInstance.execute();
//I am trying to mock clientInstance.execute
}
}
公共类类名{
公众娱乐{
HttpClient-clientInstance=null;
//clientInstance在此处初始化
response=clientInstance.execute();
//我试图模拟clientInstance.execute
}
}
您可以使用Mockito.mock为clientInstance创建一个mock实例,然后使用Mockito定义它返回的内容。当时,如下所示:
HttpClient clientInstance = Mockito.mock(HttpClient.class); // initialize mock
Object mockReturn = new Object(); // define your return object
Mockito.when(clientInstance.execute()).thenReturn(mockReturn); // define what mock returns
Object response = clientInstance.execute(); // call your mock
如前所述,您最好重新设计代码以实现可测试性:
public class ClassName {
public static <type> fun(HttpClient clientInstance){
response = clientInstance.execute();
//I am trying to mock response.execute
}
}
如果HttpClient
初始化很棘手,您想测试它,该怎么办?您可以将其重构为另一种方法:
public class ClassName {
/**
* Package-private for testing reasons.
* Do not call it from within the class in production code!
*/
static HttpClient createHttpClient() {
HttpClient clientInstance = null;
// clientInstance initialised here
return clientInstance;
}
/* this method is *not* going to be tested */
public static <type> fun(HttpClient clientInstance){
HttpClient clientInstance = createHttpClient();
return fun(clientInstance);
}
/* this method is going to be tested */
public static <type> fun(HttpClient clientInstance){
response = clientInstance.execute();
// mocking clientInstance.execute() will be easy now
}
}
公共类类名{
/**
*出于测试原因,包是私有的。
*不要在生产代码中从类内部调用它!
*/
静态HttpClient createHttpClient(){
HttpClient-clientInstance=null;
//clientInstance在此处初始化
返回客户端实例;
}
/*此方法*不*将被测试*/
公共静态乐趣(HttpClient客户端实例){
HttpClient clientInstance=createHttpClient();
返回乐趣(clientInstance);
}
/*这一方法将得到检验*/
公共静态乐趣(HttpClient客户端实例){
response=clientInstance.execute();
//模仿clientInstance.execute()现在很容易了
}
}
如果其他所有操作都失败,您可以使用模拟构造函数。我从未见过值得费心的案子。无论如何,解释了如何使用它。您不能。您需要将clientInstance注入fun()
方法或使其成为一个实例变量。因此,除了修改上述函数之外,没有办法模拟clientInstance.execute()。当((Mockito.any(HttpClient.class.execute())。然后返回(httpResponse)
它会工作吗?遗憾的是,问题没有解决“如何使用Mockito?”或“如何修改代码使其可测试?”。
public class ClassName {
/**
* Package-private for testing reasons.
* Do not call it from within the class in production code!
*/
static HttpClient createHttpClient() {
HttpClient clientInstance = null;
// clientInstance initialised here
return clientInstance;
}
/* this method is *not* going to be tested */
public static <type> fun(HttpClient clientInstance){
HttpClient clientInstance = createHttpClient();
return fun(clientInstance);
}
/* this method is going to be tested */
public static <type> fun(HttpClient clientInstance){
response = clientInstance.execute();
// mocking clientInstance.execute() will be easy now
}
}