Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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 Powermock-模拟超级方法调用_Java_Mockito_Powermock_Super - Fatal编程技术网

Java Powermock-模拟超级方法调用

Java Powermock-模拟超级方法调用,java,mockito,powermock,super,Java,Mockito,Powermock,Super,这是我的密码- 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 {

这是我的密码-

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());
    }

}
我想在
ChildService
类中模拟
super.save()
。但是我找不到一个办法
suppressMethod()
仅抑制并返回默认值(在上述情况下为0)。像
MemberModifier
Stubber
MethodProxy
这样的东西只对静态方法有效

在Powermock中有这样做的方法吗


我使用的是Powermock 1.5和Mockito 1.9.5。

看来jMockit可以满足我的需要。也许我会把这个问题发布到powermock邮件列表中。同时,下面应该足够了。 打包学习模拟工具。学习模拟工具; 打包学习模拟工具。学习模拟工具

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();
    }

}

对我来说,这种情况告诉我的是使用组合而不是继承!这可能也有助于更好的OO设计。所以我甚至不用费心用powermock来实现这一点,就像现在就开始编写遗留代码一样!请参阅(除此之外)以获得有关PowerMock是否可以这样做的最终答案。jMockit删除了版本1.4中的@MockClass(realClass)注释(从2013年8月起)