java 如何使用 PowerMockito 从最终静态类返回模拟对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11408509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:58:22  来源:igfitidea点击:

How do I use PowerMockito to return a mock object from a final static class

javatestngmockitopowermock

提问by user1513941

Hey I'm using Mockito and TestNG to write a unit test for a class that's making a lot of external calls to a service, I'm actually quite new to this hence I seem to be stuck with little documentation on the net for my exact problem.

嘿,我正在使用 Mockito 和 TestNG 为一个类编写单元测试,该类对服务进行了大量外部调用,我实际上对此很陌生,因此我似乎在网上几乎没有关于我的确切文档的文档问题。

my test looks like this basically

我的测试基本上是这样的

@Test
public class ClassToTestTest{

@Mock
private Object1 object1;

@Mock
private Object2 object2;

@InjectMocks
private ClassToTest classToTest;

public void test1(){
    classToTest.methodToTest();

}
...
...
}

The actual class is as follows

实际类如下

import FinalClass;

public class ClassToTest{

private Object1 object1;
private Object2 object2;

public void methodToTest(){
    object2 = FinalClass.getObject2();

    ...
    ...
}

...
...
}

I just need FinalClass.getObject2() to return the mock of Object2 That I've created in my Test, I know I can mock FinalClass using PowerMock, but I'm not quite getting how to inject it in the classToTest that I've created, so that when I run the classToTest.methodToTest() from my test object2 is initialized with my mocked implementation.

我只需要 FinalClass.getObject2() 来返回我在测试中创建的 Object2 的模拟,我知道我可以使用 PowerMock 来模拟 FinalClass,但我不太了解如何将它注入到我的 classToTest 中创建,以便当我从我的测试 object2 运行 classToTest.methodToTest() 时使用我的模拟实现进行初始化。

Thanks in Advance!

提前致谢!

回答by Brad

You're missing the annotation @PrepareForTest and the use of mockStatic() which are required when you wish to mock a final static class with PowerMockito. I think where you might be getting confused is that you're dealing with a final staticclass and not only a finalclass, so there are a few additional mock calls you need to be aware of.

当您希望使用 PowerMockito 模拟最终静态类时,您缺少注释 @PrepareForTest 和 mockStatic() 的使用。我认为您可能会感到困惑的地方在于您正在处理一个final static类而不仅仅是一个final类,因此您需要注意一些额外的模拟调用。

Given these implementation classes

鉴于这些实现类

/*
The final static class that's giving you all the mocking grief
*/
public final class FinalStaticClass {

    // this object instance "will not" be returned when we mock() the class
    private static MyObject obj = new MyObject();

    public static MyObject getMyObject() {
        return obj; 
    }
}

/*
A simple value object used by MyClass
*/
public class MyObject {

}

/*
The class you wish to test
*/
public class MyClass {

    private MyObject obj;

    public void methodToTest() {
        obj = FinalStaticClass.getMyObject();
        // do something
    }

    public MyObject getMyObject() {
        return obj;
    }
}

Make sure you include powermock-mockito-1.4.10-full.jarin your project, then you can use this test class

确保你包含powermock-mockito-1.4.10-full.jar在你的项目中,然后你可以使用这个测试类

import static org.junit.Assert.assertEquals;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import ...    

@RunWith(PowerMockRunner.class)
@PrepareForTest(FinalStaticClass.class)
public class MyClassTest{

    @Mock
    MyObject expectedObject;

    @InjectMocks
    MyClass myClass = new MyClass();

    @Test
    public void test1(){

        // mock all static methods
        mockStatic(FinalStaticClass.class);

        when(FinalStaticClass.getMyObject()).thenReturn(expectedObject);

        // execute the method under test
        myClass.methodToTest();

        assertEquals(expectedObject, myClass.getMyObject());

    }
}

You can see that expectedObjectis a mock that you have created in the test and not the implementation returned from FinalStaticClass

您可以看到这expectedObject是您在测试中创建的模拟,而不是从 FinalStaticClass 返回的实现