Powermock - 嘲讽超级方法调用(Powermock - mocking a super

2019-07-17 18:56发布

这里是我的代码 -

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;

import org.powermock.core.classloader.annotations.*;
import static org.powermock.api.support.SuppressCode.*;

class BaseService {
    public int save() {
        validate();
        return 2;
    }

    public static int save2() {
        return 5;
    }

    public void validate() {
        System.out.println("base service save executing...");
    }
}

class ChildService extends BaseService {
    public int save() {
        System.out.println("child service save executing...");
        int x = super.save2();
        int y = super.save();
        System.out.println("super.save returned " + y);
        load();
        return 1 + x;
    }

    public void load() {
        System.out.println("child service load executing...");
    }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(BaseService.class)
public class PreventSuperInvocation {

    @Test
    public void testSave() throws Exception {

        org.powermock.api.support.Stubber.stubMethod(BaseService.class,
                "save2", 4);
        suppressMethod(BaseService.class, "save");
        ChildService childService = new ChildService();
        System.out.println(childService.save());
    }

}

我想嘲笑super.save()ChildService类。 但我不能找到这样做的一种方式。 suppressMethod()只抑制和(在上述情况下为0)返回默认值。 事情就是这样MemberModifierStubberMethodProxy只有静态方法的工作。

是否有Powermock这样做的呢?

我使用Powermock 1.5和1.9.5的Mockito。

Answer 1:

看来,jMockit可以做什么,我需要。 也许我我会后就此问题向powermock邮件列表。 与此同时以下就足够了。 包learning_mocking_tools.learning_mocking_tools; 包learning_mocking_tools.learning_mocking_tools;

import mockit.*;

import org.junit.Assert;
import org.junit.Test;


class BaseService {
    public int save() {
        validate();
        return 2;
    }

    public static int save2() {
        return 5;
    }

    public void validate() {
        System.out.println("base service save executing...");
    }
}

class ChildService extends BaseService {
    public int save() {
        System.out.println("child service save executing...");
        int x = super.save2();
        int y = super.save();
        System.out.println("super.save returned " + y);
        load();
        return 1 + y;
    }

    public void load() {
        System.out.println("child service load executing...");
    }
}

@MockClass(realClass = BaseService.class)
class MockBase {

    @Mock
    public int save() {
        System.out.println("mocked base");
        return 9;
    }
}

public class PreventSuperInvocation {

    @Test
    public void testSave() throws Exception {
        MockBase mockBase = new MockBase();
        Mockit.setUpMock(BaseService.class, mockBase);

        ChildService childService = new ChildService();
//      int x = childService.save();

        Assert.assertEquals(9 + 1, childService.save());

        Mockit.tearDownMocks();
    }

}


文章来源: Powermock - mocking a super method invocation