Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java/Mockito中模拟链式方法调用_Java_Unit Testing_Mockito_Powermock - Fatal编程技术网

在Java/Mockito中模拟链式方法调用

在Java/Mockito中模拟链式方法调用,java,unit-testing,mockito,powermock,Java,Unit Testing,Mockito,Powermock,我在当前项目中使用Mockito模拟服务。 我有一个场景需要在代码中模拟链式方法。链式方法使用fluent设计模式。代码如下。我找不到满足我要求的解决方案 ProcessCall setValue = ProcessCall.url("").http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create(); 我试图模仿上面的代码,如下所示 @Test public void ProcessP

我在当前项目中使用
Mockito
模拟服务。 我有一个场景需要在代码中模拟链式方法。链式方法使用fluent设计模式。代码如下。我找不到满足我要求的解决方案

ProcessCall setValue = ProcessCall.url("").http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create();
我试图模仿上面的代码,如下所示

   @Test
   public void ProcessPost(){
    System.out.println("--------------------------");
    ProcessCall procCall= Mockito.mock(ProcessCall.class, Mockito.RETURNS_DEEP_STUBS);
    Mockito.when(ProcessCall .url("").http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create()).thenReturn(??);   

}
不确定在Return(??)method.ProcessCall中传递什么是具有私有构造函数的类。它有一个execute()方法,我需要根据调用的结果执行它。 我得到以下错误:

  org.mockito.exceptions.misusing.MissingMethodInvocationException: 
  when() requires an argument which has to be 'a method call on a mock'.
  For example:
    when(mock.getArticles()).thenReturn(articles);
 Or 'a static method call on a prepared class`
 For example:
    @PrepareForTest( { StaticService.class }) 
     TestClass{
       public void testMethod(){
          PowerMockito.mockStatic(StaticService.class);
         when(StaticService.say()).thenReturn(expected);
     }
   }

   Also, this error might show up because:
  1. inside when() you don't call method on mock but on some other object.
  2 . inside when() you don't call static method, but class has not been prepared.
有人能帮我解决这个问题吗。我被这个问题困住了,因此找不到任何适当的解决办法


谢谢

我认为例外情况最能说明解决方案。。在模拟静态方法时,建议使用PowerMockito(您需要添加适当的依赖项),然后在测试中:

@RunWith(PowerMockRunner.class)
@PrepareForTest( { ProcessCall.class }) 
public class MyTest{

    @Test
    public void ProcessPost(){
       System.out.println("--------------------------");
       ProcessCall processCallInstance = ProcessCall.getInstance();
       ProcessCall procCall= PowerMockito.mockStatic(ProcessCall.class
                 , Mockito.RETURNS_DEEP_STUBS);
       Mockito.when(ProcessCall .url("").http(HttpMethod.GET)
                   .contentType(null).reqHeaders(null).payload(null).create())
              .thenReturn(processCallInstance);  
       ...
       processCallInstance.execute();
       ...
}
我假设
ProcessCall
是一个单例,您需要使用类似
ProcessCall.getInstance()的东西
获取一个对象,然后将其标记为深存根调用的结果。。然后在上面执行你需要的任何东西

另外

如果要模拟
execute()
方法,那么同样可以使用PowerMockito来实现这一点:

@RunWith(PowerMockRunner.class)
@PrepareForTest( { ProcessCall.class }) 
public class MyTest{

    @Test
    public void ProcessPost(){
       System.out.println("--------------------------");
       ProcessCall processCallInstance = PowerMockito.mock(ProcessCall.class);

除了“使用powermock进行静态调用”;还考虑“不使用静态调用,因此不需要PrimeCK”。如果你有选择,在这里选择选项2;永远如此!谢谢但不幸的是,powermock不支持调用静态方法。仍然面临同样的问题,但我感谢您的帮助。请尝试使用PowerMockito.doReturn(processCallInstance).when(ProcessCall).url(“”).http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create();