Mock of static method with Demoiselle and PowerMock / Mockito

6

I'm trying to mock a static method, but PowerMock requires the use of a specific Runner (PowerMockRunner) for mock static to work!

And for Demoiselle to work you have to start Weld with DemoiselleRunner, but the JUnit API only accepts a single Runner.

@RunWith(PowerMockRunner.class) //DemoiselleRunner.class
@PrepareForTest(Fabrica.class)
public class MeuBCTest {

    @Inject
    private PrimeiroBC primeiroBC; //Não injeta com o PowerMockRunner
    private SegundoBC segundoBC;

    @Before
    public void setUp() {
        primeiroBC = Mockito.spy(primeiroBC);
        segundoBC = Mockito.mock(SegundoBC.class);
    }

    @Test
    public void testCamposObrigatoriosComSucesso() {

        PowerMockito.mockStatic(Fabrica.class);
        PowerMockito.when(Fabrica.createSegundoBC()).thenReturn(segundoBC);

        primeiroBC.fazerAlgo(); //Internamente este método chama o Fabrica.createSegundoBC() para executar outra operação.

        //verificações

    }
}

Initially I thought about creating my own PowerMockRunner Runner by extending and adding Weld as it is done in DemoiselleRunner.

Is there a setup in Demoiselle or PowerMock so you do not need to create another Runner?

    
asked by anonymous 11.02.2014 / 14:34

2 answers

2

I've never used the Demoiselle Framework. However, the difficulty with multiple Runners is not uncommon and occurs with other frameworks.

One solution is to use the PowerMockRule . According to the documentation:

  

Since version 1.4 you can start PowerMock using a JUnit rule ( JUnit Rule ) instead of PowerMockRunner and annotation RunWith . (free translation)

Example usage:

@RunWith(DemoiselleRunner.class)
@PrepareForTest(Fabrica.class)
public class MeuBCTest {

     @Rule
     PowerMockRule rule = new PowerMockRule();

     ...

}
    
11.02.2014 / 14:54
0

I already had a case with Arquilian , and I used PowerMockRule also, but in this case Runner of Demoiselle was not used. In Demoiselle we follow the JUnit orientation that is to extend BlockJUnit4ClassRunner . If it does not work with PowerMockRule perhaps the solution is, as you commented, create your own Runner . Who knows, you can not create DemoiselleRunnerPowerMock ? And if you do not succeed in creating yourself you can open a case at: frameworkdemoiselle.gov.br

NOTE: The utluiz commented that you gave up Demoiselle because of commits frequencies . Has your review been in the GitHub repository ? Because we are no longer using SourceForge SVN after version 2.

    
12.02.2014 / 18:40