<small id='3GSxO'></small><noframes id='3GSxO'>

<tfoot id='3GSxO'></tfoot>
    • <bdo id='3GSxO'></bdo><ul id='3GSxO'></ul>
  1. <legend id='3GSxO'><style id='3GSxO'><dir id='3GSxO'><q id='3GSxO'></q></dir></style></legend>

      <i id='3GSxO'><tr id='3GSxO'><dt id='3GSxO'><q id='3GSxO'><span id='3GSxO'><b id='3GSxO'><form id='3GSxO'><ins id='3GSxO'></ins><ul id='3GSxO'></ul><sub id='3GSxO'></sub></form><legend id='3GSxO'></legend><bdo id='3GSxO'><pre id='3GSxO'><center id='3GSxO'></center></pre></bdo></b><th id='3GSxO'></th></span></q></dt></tr></i><div id='3GSxO'><tfoot id='3GSxO'></tfoot><dl id='3GSxO'><fieldset id='3GSxO'></fieldset></dl></div>
    1. 如何在 Java 中模拟静态方法?

      时间:2023-09-25
        • <bdo id='C97Jn'></bdo><ul id='C97Jn'></ul>

            <small id='C97Jn'></small><noframes id='C97Jn'>

          1. <legend id='C97Jn'><style id='C97Jn'><dir id='C97Jn'><q id='C97Jn'></q></dir></style></legend>

                  <tbody id='C97Jn'></tbody>
                <tfoot id='C97Jn'></tfoot>
                <i id='C97Jn'><tr id='C97Jn'><dt id='C97Jn'><q id='C97Jn'><span id='C97Jn'><b id='C97Jn'><form id='C97Jn'><ins id='C97Jn'></ins><ul id='C97Jn'></ul><sub id='C97Jn'></sub></form><legend id='C97Jn'></legend><bdo id='C97Jn'><pre id='C97Jn'><center id='C97Jn'></center></pre></bdo></b><th id='C97Jn'></th></span></q></dt></tr></i><div id='C97Jn'><tfoot id='C97Jn'></tfoot><dl id='C97Jn'><fieldset id='C97Jn'></fieldset></dl></div>

                本文介绍了如何在 Java 中模拟静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

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

                1) 它应该调用 BlockAbstractFactory

                2) 它应该从子类方法 getBlocks()

                2) it should populate variable blockList from the subclass method getBlocks()

                3) 它应该调用一个静态方法 createFile 从一个最终的帮助类 FileHelper 传递一个字符串参数

                3) it should call a static method createFile from a final helper class FileHelper passing a String parameter

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

                4) it should call the run method of each BlockController in the blockList

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

                So far, I have this empty method:

                public class FileGenerator {
                    // private fields with Getters and Setters
                
                    public void generateBlocks() {
                    }
                }
                

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

                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).

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

                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.

                如何测试静态方法是否在 fileGerator.generateBlocks() 中被调用?

                How can I test if the static method is called within 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());
                    }
                }
                

                推荐答案

                我没有使用 PowerMock 的经验,但是由于您还没有得到答案,所以我只是在阅读文档以查看是否可以帮助您有点在路上.

                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:

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

                这里您可以找到更多信息.

                这有帮助吗?

                这篇关于如何在 Java 中模拟静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                <legend id='u49Us'><style id='u49Us'><dir id='u49Us'><q id='u49Us'></q></dir></style></legend>
                  <tbody id='u49Us'></tbody>

                <i id='u49Us'><tr id='u49Us'><dt id='u49Us'><q id='u49Us'><span id='u49Us'><b id='u49Us'><form id='u49Us'><ins id='u49Us'></ins><ul id='u49Us'></ul><sub id='u49Us'></sub></form><legend id='u49Us'></legend><bdo id='u49Us'><pre id='u49Us'><center id='u49Us'></center></pre></bdo></b><th id='u49Us'></th></span></q></dt></tr></i><div id='u49Us'><tfoot id='u49Us'></tfoot><dl id='u49Us'><fieldset id='u49Us'></fieldset></dl></div>

                <small id='u49Us'></small><noframes id='u49Us'>

                  <bdo id='u49Us'></bdo><ul id='u49Us'></ul>
                    • <tfoot id='u49Us'></tfoot>