单元测试PowerMockito使用心得


测试类模板

@RunWith(PowerMockRunner.class)
@PrepareForTest({CollaboratorWithStaticMethods.class})
public class CollaboratorWithStaticMethodsTest {
    @Test
    public void testFirstMethod() {
    }
}

静态方式调用当前类的其他静态方法时,如何写单元测试

public static String forthMethod() {
        String str = firstMethod("John");
        return str + "Good morning!";
    }
@Test
public void testForthMethod() {
    PowerMockito.spy(CollaboratorWithStaticMethods.class);
    PowerMockito.when(CollaboratorWithStaticMethods.firstMethod(anyString())).thenReturn("test");
    String result = CollaboratorWithStaticMethods.forthMethod();
    Assert.assertEquals("testGood morning!", result);
}

进入异常catch语句块


文章作者: Hiper
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Hiper !
  目录