Question

I have a few tests right now that uses PowerMock and Mockito to "mock objects". The problem is, all those tests create the same mocks over and over again. Is it possible to have an abstract class from which my test derive that pre-mocks these objects ?

Example :

public abstract class AbstractTest {
    public void initialize() {
        PowerMockito.mockStatic(StaticMock.class);
        when(StaticMock.doSomething()).thenReturn(true);
    }
}

@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticMock.class) // BanManager.class contains static methods
public class Test extends AbstractTest {

    @SuppressWarnings("serial")
    @Test
    public void testWithPerms() {
        initialize();
        SomeObject obj = new SomeObject();
        obj.doSomething();
        PowerMockito.verifyStatic();
        Static.verifyIsCalled();
    }
}

How could I do something like the above that actually worked ? This gives me Inconsistent stackmap frames

EDIT : Thank you for your answer. My problem was actually due to me using a PowerMock version incompatible with java 7. Still, your indication will be helpful.

Was it helpful?

Solution

Of course this is possible. If you put the @Before annotation on a method in the base class, it will run at the beginning of every test in any subclass. Alternatively, you can put this off in some kind of factory class that is used in all your tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top