简体   繁体   English

当父级位于不同的程序包中且子类为Abstract时,模拟受保护的父级方法

[英]Mock Protected Parent Method When the Parent is in a Different Package and the Child Class is Abstract

I need to mock a protected method in the parent class of my class under test but the parent class is in a different package. 我需要在被测类的父类中模拟一个受保护的方法,但是父类在另一个包中。 I thought that the solution would be to use PowerMockito.spy() but I cannot instantiate the type Child because it is abstract. 我以为解决方案是使用PowerMockito.spy()但是我无法实例化Child类型,因为它是抽象的。 There's gotta be a solution for this issue without refactoring. 无需重构就可以解决此问题。

Here's the Dependencies. 这是依赖关系。

        <dependency org="org.powermock" name="powermock-core" rev="1.6.4"/>
        <dependency org="org.powermock" name="powermock-module-junit4" rev="1.6.4"/>
        <dependency org="org.powermock" name="powermock-api-mockito" rev="1.6.4"/> 

This is legacy code so I cannot refactor, but here's the simplified code. 这是遗留代码,因此我无法重构,但这是简化的代码。

Parent.java Parent.java

package parent;

public class Parent {

    // Want to mock this protected parent method from different package
    protected String foo() {

        String someValue = null;

        // Logic setting someValue

        return someValue;
    }
}

Child.java Child.java

package child;

import parent.Parent;

public abstract class Child extends Parent {

    String fooString = null;

    public String boo() {

        this.fooString = this.foo();

        String booString = null;

        // Logic setting booString

        return booString;
    }
}

You can use PowerMock to mock methods that are not public 您可以使用PowerMock模拟不公开的方法

@RunWith(PowerMockRunner.class)
public class ChildTest {

   @Test
   public void testBoo() throws Exception {
      Child child = PowerMockito.mock(Child.class);
      PowerMockito.when(child, "foo").thenReturn("someValue");
      PowerMockito.doCallRealMethod().when(child).boo()
      String boo = child.boo();

      //assert here whatever you want to assert
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当父级位于其他程序包中时,模拟受保护的父级方法 - Mock Protected Parent Method When the Parent is in a Different Package 子类中与抽象父类的不同方法行为 - Different method behavior in child class from abstract parent class 受保护的抽象方法在父类的子类包装实例中不可见 - Protected abstract methods not visible in child class wrapping instance of parent 如何在Java中抽象父级的子类中初始化受保护的最终变量? - How to initialize a protected final variable in a child class of an abstract parent in Java? 使用子类和抽象父类中受保护的成员变量 - The use of protected member variables from child class and abstract parent 如何模拟父类的受保护方法? - How can I mock protected method of parent class? Java子类无法实现父类的抽象方法 - Java child class not able to implement parent class's abstract method 当变量是抽象父类时,Java将分派给子类 - Java dispatch to child class when variable is of abstract parent class Java - 为什么另一个 package 中的子级无法通过父级引用访问父级的受保护方法? - Java - Why Child in another package cannot access parent's protected method through parent reference? 当父类中的受保护(或抽象)方法被删除并且子类已实现该方法时,如何引起编译错误/警告 - How to cause a compile error/warning, when a protected (or abstract) method in the parent class is removed and sub-classes have implemented it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM