繁体   English   中英

使用Mockito和Powermock从同一类中模拟构造函数和静态方法

[英]Mocking constructor and static method from same class with Mockito and Powermock

我正在编写与第三方Java SDK接口的应用程序。 我目前正在使用Mockito和PowerMock编写单元测试。 我在模拟sdk提供的类时遇到问题。 虚拟类如下所示:

class A{
    public static A getInstance() {
     ...
    }

    public A(){
     ...
    }
}

我正在尝试模拟B类,该类同时调用A的构造函数和A中的静态方法。

class B{
    public String doSomething(){
A test1 = A.getInstance();
A test2 = new A();

...

}
}

我需要能够在B的方法中模拟test1和test2对象,因此我尝试按以下方式编写测试:

@RunWith( PowerMockRunner.class )
@PrepareForTest( B.class )

public class BTest{

    @PrepareForTest( A.class )
    @Test
    public void testdoSomething() {

        A mockedTest1 = Mockito.mock(A.class);
        A mockedTest2 = Mockito.mock(A.class);

        PowerMockito.whenNew(A.class).withAnyArguments().thenReturn(mockedTest2)
        PowerMockito.mockStatic( A.class )
        PowerMockito.when( A.getInstance()).thenAnswer(mockedTest1)


        B b = new B();
        b.doSomething();

    }

在模拟静态方法时,不会对构造方法进行模拟。 但是,如果我不尝试模拟构造函数(即,删除PrepareForTest批注并按如下所示更改代码:)

@RunWith( PowerMockRunner.class )
@PrepareForTest( B.class )

public class BTest{

    @Test
    public void testdoSomething() {

        A mockedTest1 = Mockito.mock(A.class);
        A mockedTest2 = Mockito.mock(A.class);

        PowerMockito.whenNew(A.class).withAnyArguments().thenReturn(mockedTest2)


        B b = new B();
        b.doSomething();

    }  

我可以正确构造构造函数。 Powermock是否有某些东西可以防止同时模拟构造函数和静态方法? 还是我想念的东西?

谢谢。

您已经很接近自己了,但是您必须考虑一些事项才能使所有内容都适合这个难题:

1) 用于模拟构造函数的Powermock文档

  1. 在测试用例的类级别使用@PrepareForTest( ClassThatCreatesTheNewInstance.class )批注。

2) Powermock文档,用于模拟静态方法

  1. 在测试用例的类级别使用@PrepareForTest( ClassThatContainsStaticMethod.class )批注

到目前为止,我们知道我们需要为测试A.class (静态方法)和B.class (构造函数)做准备,但是您在类级别以及测试方法级别都使用了@PrepareForTest

3) Powermock的javadoc @PrepareForTest

此注释可以放在测试类和单个测试方法上。 如果放置在一个类上,则该测试类中的所有测试方法都将由PowerMock处理(以实现可测试性)。 要覆盖单个方法的此行为,只需在特定的测试方法上放置@PrepareForTest批注。 例如,在您想在测试方法A中修改类X但在测试方法B中希望保留X不变的情况下,这很有用

总之,可以在类级别使用@PrepareForTest({A.class, B.class}) ,也可以在方法级别使用@PrepareForTest({A.class, B.class})

暂无
暂无

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

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