模拟受保护的构造函数

时间:2018-01-10 13:26:25

标签: java junit4 powermockito

我想模拟 super ()调用受保护的构造函数

为此,我使用" PowerMockito.suppress" 但它不起作用(代码调用超级构造函数,它抛出runtimeException)。

如何模拟超级调用(),因此它没有做任何事情。

    public class FooClass {

        public void someMethod(){
            String arg1 = "arg1";
            Integer arg2 =  123;
            ExtendedClass extendedClass = new ExtendedClass(arg1, arg2);
        }
    }

    public class SuperClass {
         protected SuperClass(String arg1, Integer arg2) {throw new RuntimeException();}
    }

    public class ExtendedClass extends SuperClass{

        public ExtendedClass(String arg1, Integer arg2){
            super(arg1, arg2);
         }
    }

    @RunWith(PowerMockRunner.class)
    @PowerMockRunnerDelegate(MockitoJUnitRunner.class)
    @PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.http.conn.ssl.*", "javax.net.ssl.*"})
    @PrepareForTest({SuperClass.class})
    public class TestClass extends TestCase{

        @InjectMocks
        FooClass fooClass = new FooClass();

        @Test
        public void test(){
            PowerMockito.suppress(PowerMockito.constructor(SuperClass.class, String.class, Integer.class));
            fooClass.someMethod();
        }
    }

enter image description here

2 个答案:

答案 0 :(得分:1)

你必须准备ExtendedClass for Test而不是SuperClass。

更改

 @PrepareForTest({SuperClass.class})

 PowerMockito.suppress(PowerMockito.constructor(SuperClass.class, String.class, Integer.class));

 @PrepareForTest({ExtendedClass.class})

 PowerMockito.suppress(PowerMockito.constructor(ExtendedClass.class, String.class, Integer.class));

答案 1 :(得分:1)

您在我的机器上成功通过测试:

enter image description here 你使用哪种版本的powermock和mockito?

这是我的:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.7.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.7.3</version>
    <scope>test</scope>
</dependency>

你的问题的根本原因是@pvpkiran已经写过你必须准备ExtendedClass for Test而不是SuperClass

<强>更新 请查看此链接Suppress-Unwanted-Behavior#suppress-super-class-constructors

@RunWith(PowerMockRunner.class)
@PrepareForTest({TestParentConstructor.ExtendedClass.class})
public class TestParentConstructor {

    @Before
    public void init() {
        suppress(constructor(SuperClass.class));
    }

    @Test
    public void test(){
        FooClass fooClass = new FooClass();
        fooClass.someMethod();
    }

    public class FooClass {
        public void someMethod(){
            String arg1 = "arg1";
            Integer arg2 =  123;
            ExtendedClass extendedClass = new ExtendedClass(arg1, arg2);
        }
    }

    public class SuperClass {
        protected SuperClass(String arg1, Integer arg2) {throw new RuntimeException();}
    }

    public class ExtendedClass extends SuperClass{
        public ExtendedClass(String arg1, Integer arg2){
            super(arg1, arg2);
            System.out.println("Hello from Extended!");
        }
    }
}