Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to mock static method in Java?
    primarykey
    data
    text
    <p>I have a class <code>FileGenerator</code>, and I'm writing a test for the <code>generateFile()</code> method that should do the following:</p> <p>1) it should call the static method <code>getBlockImpl(FileTypeEnum)</code> on <code>BlockAbstractFactory</code></p> <p>2) it should populate variable <code>blockList</code> from the subclass method <code>getBlocks()</code></p> <p>3) it should call a static method <code>createFile</code> from a final helper class <code>FileHelper</code> passing a String parameter</p> <p>4) it should call the run method of each <code>BlockController</code> in the blockList</p> <p>So far, I have this empty method:</p> <pre><code>public class FileGenerator { // private fields with Getters and Setters public void generateBlocks() { } } </code></pre> <p>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).</p> <p>My problem is: my first test (calling method <code>getBlockList()</code> from <code>BlockAbstractFactory</code>) is passing, even though there is no implementation in <code>generateBlocks()</code>. I have implemented the static method in <code>BlockAbstractFactory</code> (returning null, so far), to avoid Eclipse syntax errors.</p> <p>How can I test if the static method is called within <code>fileGerator.generateBlocks()</code>?</p> <p>Here's my Test Class, so far:</p> <pre><code>@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()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload