使用PowerMock模拟链式方法调用

时间:2016-02-23 09:51:46

标签: java unit-testing junit mockito powermock

我有一个类,我想用一个包含一些链式方法调用的公共静态方法来测试。假设在链式方法调用期间发生异常,我该如何有效地处理它并使它返回一些特定的值?

以下是测试类的代码示例。

@RunWith(PowerMockRunner.class)
@PrepareForTest({CodeWithPrivateMethod.class,CodeWithAnotherPrivateMethod.class,CodeWithYetAnotherPrivateMethod.class})
public class CodeWithPrivateMethodTest {

@Test
public void when_gambling_is_true_then_always_explode() throws Exception {


    CodeWithYetAnotherPrivateMethod codeWithYetAnotherPrivateMethod = PowerMockito.spy(new CodeWithYetAnotherPrivateMethod());
    PowerMockito.whenNew(CodeWithYetAnotherPrivateMethod.class).withAnyArguments().thenReturn(codeWithYetAnotherPrivateMethod);

    CodeWithAnotherPrivateMethod codeWithAnotherPrivateMethod = PowerMockito.spy(new CodeWithAnotherPrivateMethod());
    PowerMockito.whenNew(CodeWithAnotherPrivateMethod.class).withAnyArguments().thenReturn(codeWithAnotherPrivateMethod);

    PowerMockito.doReturn(true).when(codeWithYetAnotherPrivateMethod, "getGambling");

    //PowerMockito.doReturn(codeWithYetAnotherPrivateMethod).when(codeWithAnotherPrivateMethod, "getGambleValue");

    PowerMockito.spy(CodeWithPrivateMethod.class);
    CodeWithPrivateMethod.startGamble();

    }

}

以下是受测试类的代码示例

public class CodeWithPrivateMethod {

public static void startGamble() {

    Boolean gamble = CodeWithAnotherPrivateMethod.getGambleValue()
            .getGambling();
    if (gamble) {
        System.out.println("kaboom");
    }else{
        System.out.println("boom boom");
    }
    }
}

以下是从受测试类调用的类的代码示例

public class CodeWithAnotherPrivateMethod {

static CodeWithYetAnotherPrivateMethod codeWithYetAnotherPrivateMethod = new CodeWithYetAnotherPrivateMethod();


public static CodeWithYetAnotherPrivateMethod getGambleValue() {
    return codeWithYetAnotherPrivateMethod; //works fine
    return null; // fails
    }
}

以下是从被测试类调用的其他类的代码示例

public class CodeWithYetAnotherPrivateMethod {

public Boolean getGambling() {
    return false;
    }
}

假设我从CodeWithAnotherPrivateMethod类的getGambleValue()方法返回一个空值,如何在我的测试类中有效地处理这个空值?

1 个答案:

答案 0 :(得分:1)

这是使用Mockito指定预期异常的方法:

@Test(expected = NullPointerException.class)
public void when_gambling_is_true_then_always_explode() throws Exception {
    ...

在我发现这件事之前,我会这样做:

@Test
public void when_gambling_is_true_then_always_explode() throws Exception {
    // setup omitted
    try {
        CodeWithPrivateMethod.startGamble();
    } 
    catch(NullPointerException e) {
        // expected
        return;
    }
    fail("Expected NullPointerException");
}

编辑:测试多个静态调用彼此的类是严重的代码味道。单元测试应测试单个类,内联静态调用应限于实用程序类。

另一条评论:您的示例类名称非常混乱。下次请坚持使用Foo,Bar,Baz或Appple,Pear,Banana。

如果你没有获得NPE,那么我希望你的嘲弄/间谍干扰。如果您在没有模拟/间谍的情况下调用测试代码,则调用链将是:

CodeWithPrivateMethod.startGamble();
->
CodeWithYetAnotherPrivateMethod value = CodeWithAnotherPrivateMethod.getGambleValue();
-> 
return null;
<-
value.getGambling();
<- throws NullPointerException

你到底想要找到什么或实现什么?

编辑:以下是它应该如何与PowerMock一起使用

@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithAnotherPrivateMethod.class)
public class CodeWithPrivateMethodTest {

  @Mock
  private CodeWithYetAnotherPrivateMethod yetAnotherInstance;

  @Test
  public final void testStartGamble() {

    // SETUP
    mockStatic(CodeWithAnotherPrivateMethod.class);
    expect(CodeWithAnotherPrivateMethod.getGambleValue())
        .andReturn(yetAnotherInstance);
    Boolean gamblingValue = true;
    expect(yetAnotherInstance.getGambling()).andReturn(gamblingValue);
    replayAll();

    // CALL
    CodeWithPrivateMethod.startGamble();

    // VERIFY
    verifyAll();
  }