简体   繁体   English

模拟一个受保护的方法,该方法是扩展抽象类的一部分

[英]Mock a protected method which is part of a extended abstract class

Class A A级

public abstract class A{
  protected C _c = null;

  public A(){
  }

  public B(C c){
    _c=c;
  }
}

Class B B级

public class B extends A{

 public B(C c){
   super(c);
 }

 public String dosomething(){
   this._c.dosomethingelse();
   return String s;
 }

}

I am trying to unit test dosomething(). 我正在尝试对dosomething()进行单元测试。 i am running in to an issue where as in this._c is null(Throws a null pointer exceptionat this line) i am new to mockito and unit testing not sure on how to make it skip that step so that it runs the other logic. 我遇到了一个问题,如this._c为null(在此行引发null指针异常),我是mockito和单元测试的新手,不知道如何使其跳过该步骤,以便运行其他逻辑。 i am using Mockito and PowerMockito 我正在使用Mockito和PowerMockito

Assuming you are using spring-test 假设您正在使用弹簧测试

In your init try something like this : 在您的init中尝试这样的事情:

//Create instance of C (or get already existing one)    
C myC = new C();
ReflectionTestUtils.setField(this., "_c", myC);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM