PowerMockito模拟单个静态方法和返回对象 [英] PowerMockito mock single static method and return object

查看:383
本文介绍了PowerMockito模拟单个静态方法和返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从包含2个静态方法m1和m2的类中模拟静态方法m1。我希望方法m1返回一个对象。

I want to mock a static method m1 from a class which contains 2 static methods, m1 and m2. And I want the method m1 to return an object.

我尝试了以下

1)

PowerMockito.mockStatic(Static.class, new Answer<Long>() {
         @Override
         public Long answer(InvocationOnMock invocation) throws Throwable {
            return 1000l;
         }
      });

这是调用m1和m2,它们具有不同的返回类型,因此它给出了一个返回类型不匹配错误。

This is calling both m1 and m2, which has a different return type, so it gives a return type mismatch error.

2) PowerMockito.when(Static.m1(param1,param2))。thenReturn(1000l);
但是当执行m1时不会调用它。

2) PowerMockito.when(Static.m1(param1, param2)).thenReturn(1000l); But this is not called when m1 is executed.

3) PowerMockito.mockPartial(Static.class,m1 );
给出了mockPartial不可用的编译器错误,我从 http://code.google.com/p/powermock/wiki/MockitoUsage

推荐答案

你想要做的是1部分和2部分的组合。

What you want to do is a combination of part of 1 and all of 2.

你需要使用PowerMockito.mockStatic来启用静态模拟所有类的静态方法。这意味着使可能使用when-thenReturn语法对它们进行存根。

You need to use the PowerMockito.mockStatic to enable static mocking for all static methods of a class. This means make it possible to stub them using the when-thenReturn syntax.

但是你正在使用的mockStatic的2参数重载当你调用一个你没有在mock实例上明确存根的方法时,Mockito / PowerMock应该做什么的默认策略。

But the 2-argument overload of mockStatic you are using supplies a default strategy for what Mockito/PowerMock should do when you call a method you haven't explicitly stubbed on the mock instance.

来自 javadoc



交互的答案创建具有指定策略的类模拟。它是非常先进的功能,通常你不需要
来编写体面的测试。但是,在使用
遗留系统时它会很有用。这是默认答案,所以只有当
没有存根方法调用时才会使用它。

Creates class mock with a specified strategy for its answers to interactions. It's quite advanced feature and typically you don't need it to write decent tests. However it can be helpful when working with legacy systems. It is the default answer so it will be used only when you don't stub the method call.

default 默认存根策略只是为对象,数字和布尔值方法返回null,0或false。通过使用2-arg重载,你会说不,不,不,默认情况下使用此Answer子类'的答案方法来获取默认值。它返回一个Long,所以如果你有静态方法返回一些不兼容的东西很长,有一个问题。

The default default stubbing strategy is to just return null, 0 or false for object, number and boolean valued methods. By using the 2-arg overload, you're saying "No, no, no, by default use this Answer subclass' answer method to get a default value. It returns a Long, so if you have static methods which return something incompatible with Long, there is a problem.

相反,使用1-arg版本的mockStatic来启用静态方法的存根,然后使用when-thenReturn来指定要做什么一种特殊的方法。例如:

Instead, use the 1-arg version of mockStatic to enable stubbing of static methods, then use when-thenReturn to specify what to do for a particular method. For example:

import static org.mockito.Mockito.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

class ClassWithStatics {
  public static String getString() {
    return "String";
  }

  public static int getInt() {
    return 1;
  }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithStatics.class)
public class StubJustOneStatic {
  @Test
  public void test() {
    PowerMockito.mockStatic(ClassWithStatics.class);

    when(ClassWithStatics.getString()).thenReturn("Hello!");

    System.out.println("String: " + ClassWithStatics.getString());
    System.out.println("Int: " + ClassWithStatics.getInt());
  }
}

字符串值静态方法存根以返回 Hello!,而int值静态方法使用默认存根,返回0。

The String-valued static method is stubbed to return "Hello!", while the int-valued static method uses the default stubbing, returning 0.

这篇关于PowerMockito模拟单个静态方法和返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆