Mock Static method with Demoiselle and Powermock/Mockito

Asked

Viewed 944 times

6

I’m trying to mock a Static method, but Powermock requires the use of a specific Runner (Powermockrunner) for the Static mock to work!

And for Demoiselle to work, you need 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 of creating my own Runner extending from Powermockrunner and adding Weld as it is done in Demoisellerunner.

There are some settings in Demoiselle or Powermock so you don’t need to create another Runner?

2 answers

2

I have 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 it is possible to start Powermock using a Junit rule (Junit Rule) instead of PowerMockRunner and the annotation RunWith. (free translation)

Example of use:

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

     @Rule
     PowerMockRule rule = new PowerMockRule();

     ...

}
  • It worked in part... we still needed to add the annotatio @Powermockignore({"org.mockito. *"}) in the class due to a bug (https://groups.google.com/forum/#! topic/powermock/Rupbz4ufg-w).

  • @user3297199 But after adding this annotation the test was successfully run?

  • Not yet due to Powermock, it apparently runs the tests on a separate thread from where Demoiselle starts Weld. CDI is not working. Have any hints?

  • Regarding the frequency of Demoiselle commits, the project has been on Github for a long time and no longer on SF.net SVN. You can find the updated data here: https://github.com/demoiselle/framework/network

  • @zyc I evaluated the framework at the end of 2013 and at the time found no references to Github. Actually this was not the only reason I didn’t use it. I’m accessing the framework’s official website now and there’s a lot of outdated information. It took me about 20 minutes to find out about the latest releases and I couldn’t even access the Quickstart guide in the documentation. You really need to browse the Sourceforge directories to find this information?

0

I once had an affair with Arquilian, and used the PowerMockRule also, but in this case was not used the Runner of Demoiselle. In the Demoiselle we follow the guidance of JUnit which is to extend BlockJUnit4ClassRunner. If it doesn’t work with the PowerMockRule maybe the solution is, as you commented, to create your own Runner. Who knows can not create a DemoiselleRunnerPowerMock? And if you don’t succeed alone in creating can open a case in: frameworkdemoiselle.gov.br

OBS: The utluiz commented that gave up the Demoiselle because of the frequencies of commits. Your analysis was in the repository of Github? Because we are no longer using Sourceforge SVN after version 2.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.