使用PowerMockito测试方法时跳过超级调用

时间:2013-10-18 08:29:06

标签: java testing powermock

是否可以跳过超级方法(“super.start()”)进行测试,例如:

public void start() {
   super.start();
   //other methods which I really want to test
}

编辑:忘了提 - 我不想改变原始代码。

2 个答案:

答案 0 :(得分:0)

我会将其他方法移到单独的方法

public void start() {
   super.start();
   start0();
}

void start0() {
  //other methods which you really want to test
}

这会使课程更易于测试

答案 1 :(得分:0)

感觉有可能,请查看示例:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Example.B.class)
public class Example {

  @Test
  public void testSuppressMethod() throws Exception {
    suppress(method(A.class, "doA"));
    new B().doA();
  }

  static class A {
    void doA() {
        System.out.println("a");
    }
  }

  static class B extends A {
    @Override
    void doA() {
        super.doA();
        System.out.println("b");
    }
  }
}

您只需要在suppress语句中指定父类。 使用PowerMock 1.5.1。

相关问题