Skip to content

2.0.0-beta - Mockito API methods available via interfaces (without static imports)

Pre-release
Pre-release
Compare
Choose a tag to compare
@szpak szpak released this 28 Nov 23:51
· 127 commits to master since this release

The new Mockito API methods available via interfaces allow to use methods from Mockito API without the need to use static imports. It is enough to make your test class implement WithBDDMockito interface to have all methods from stubbing/mockito Mockito API available directly.

Please note: Versions 2.x are for 2.0.22-beta and newer. See versions 1.x for Mockito 1.10.x and earlier Mockito 2 betas.

//no need to use static imports!

public class SpaceShipTest implements WithBDDMockito {

    @Test
    public void shouldVerifyMethodExecution() {
        //given
        TacticalStation tsSpy = spy(TacticalStation.class);
        willDoNothing().given(tsSpy).fireTorpedo(2);
        //when
        tsSpy.fireTorpedo(2);
        tsSpy.fireTorpedo(2);
        //then
        then(tsSpy).should(times(2)).fireTorpedo(2);
    }
}

The same code would work fine with a bunch of static imports. Of course they can be hidden in IDE and usually do not disturb much. Nevertheless to be able to write just a method name (e.g. mock(TacticalStation.class)) without a class is it required to press ALT-ENTER (in IntelliJ IDEA) to add each static import on the first usage of a given method in a test class. The situation is even worse in Eclipse where it is required to earlier add BDDMockito to "Favorites" in "Content Assist" to make it suggested by IDE.

Mockito methods are provided by 3 base interfaces, being an entry point for given set of methods: