如何在 Java 中模拟静态方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15439592/
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 19:35:32  来源:igfitidea点击:

How to mock static method in Java?

javajunittddmockitopowermock

提问by Tarek

I have a class FileGenerator, and I'm writing a test for the generateFile()method that should do the following:

我有一个 class FileGenerator,我正在为generateFile()应该执行以下操作的方法编写一个测试:

1) it should call the static method getBlockImpl(FileTypeEnum)on BlockAbstractFactory

1)它应该调用静态方法getBlockImpl(FileTypeEnum)BlockAbstractFactory

2) it should populate variable blockListfrom the subclass method getBlocks()

2)它应该blockList从子类方法填充变量getBlocks()

3) it should call a static method createFilefrom a final helper class FileHelperpassing a String parameter

3) 它应该createFileFileHelper传递 String 参数的最终助手类调用静态方法

4) it should call the run method of each BlockControllerin the blockList

4)它应该调用BlockControllerblockList中每个的run方法

So far, I have this empty method:

到目前为止,我有这个空方法:

public class FileGenerator {
    // private fields with Getters and Setters

    public void generateBlocks() {
    }
}

I am using JUnit, Mockito to mock objects and I've tried using PowerMockito to mock static and final classes (which Mockito doesn't do).

我正在使用 JUnit、Mockito 来模拟对象,并且我尝试使用 PowerMockito 来模拟静态和最终类(Mockito 不这样做)。

My problem is: my first test (calling method getBlockList()from BlockAbstractFactory) is passing, even though there is no implementation in generateBlocks(). I have implemented the static method in BlockAbstractFactory(returning null, so far), to avoid Eclipse syntax errors.

我的问题是:我的第一个测试(getBlockList()从调用方法BlockAbstractFactory)正在通过,即使generateBlocks(). 我已经在BlockAbstractFactory(返回 null,到目前为止)中实现了静态方法,以避免 Eclipse 语法错误。

How can I test if the static method is called within fileGerator.generateBlocks()?

如何测试静态方法是否在内部调用fileGerator.generateBlocks()

Here's my Test Class, so far:

到目前为止,这是我的测试课程:

@RunWith(PowerMockRunner.class)
public class testFileGenerator {
    FileGenerator fileGenerator = new FileGenerator();

    @Test
    public void shouldCallGetBlockList() {
            fileGenerator.setFileType(FileTypeEnum.SPED_FISCAL);

            fileGenerator.generateBlocks();

            PowerMockito.mockStatic(BlockAbstractFactory.class);
            PowerMockito.verifyStatic();
            BlockAbstractFactory.getBlockImpl(fileGenerator.getFileType());
    }
}

回答by bas

I have no experience with PowerMock, but since you didn't get an answer yet I'm just been reading through the documentation to see if I can help you a bit on your way.

我没有使用 PowerMock 的经验,但由于您还没有得到答案,我只是在通读文档,看看我是否可以帮助您。

I found that you need to prepare PowerMock so that I knows which static methods it needs to prepare to be mocked. Like so:

我发现你需要准备 PowerMock 以便我知道它需要准备哪些静态方法来模拟。像这样:

@RunWith(PowerMockRunner.class)
@PrepareForTest(BlockAbstractFactory.class) // <<=== Like that
public class testFileGenerator {
    // rest of you class
}

Hereyou can find more information.

在这里您可以找到更多信息。

Does that help?

这有帮助吗?

回答by cahen

Working example:

工作示例:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassStaticA.class, ClassStaticB.class})
public class ClassStaticMethodsTest {

    @Test
    public void testMockStaticMethod() {
        PowerMock.mockStatic(ClassStaticA.class);
        EasyMock.expect(ClassStaticA.getMessageStaticMethod()).andReturn("mocked message");
        PowerMock.replay(ClassStaticA.class);
        assertEquals("mocked message", ClassStaticA.getMessageStaticMethod());
    }