Related Projects

Brice Dutheil edited this page Aug 31, 2016 · 5 revisions

JUnit 5 mockito extension

This is an extension written by the JUnit team that allows to create and inject mocks in the test (and set up / tear down) methods.

@ExtendWith(MockitoExtension.class)
class MockitoExtensionInBaseClassTest {

    @Mock
    private NumberGenerator numberGenerator;

    @BeforeEach
    void initialize(@Mock MyType myType, TestInfo testInfo) {
        when(myType.getName()).thenReturn(testInfo.getDisplayName());
        when(numberGenerator.next()).thenReturn(42);
    }

    @Test
    void firstTestWithInjectedMock(@Mock MyType myType) {
        assertEquals("firstTestWithInjectedMock(MyType)", myType.getName());
        assertEquals(42, numberGenerator.next());
    }
}

The project is located here

Springockito

A Spring framework extension that simplifies creation of the mocks in the spring xml context files, ex:

<mockito:mock class="java.util.Date" id="mockedDate" />
<mockito:spy id="beanToBeSpied" />

The project homepage

Springockito-annotations

An Spring framework extension that pushes former approach even further - all the configuration is annotations based - no need to create special mock overriding contexts. More info here

Mockito-collections

Project to investigate the possibility of injecting objects into Collections using generics to determine which objects and mock especially can be placed into the Collections.

@InjectMocks private MyDelegate delegate;
@Mock private MyListener listener1;
@Mock private MyListener listener2;

@Before public void setup() { MockitoCollectionAnnotations.inject(this);}

See here to read more about it.

Spock Subjects-Collaborators Extension

Spock subject-collaborators extension is a port of the Mockito's @InjectMocks functionality for Spock.

It allows you to annotate your objects (most preferably system under test) with @Subject annotation (In Mockito it would be @InjectMocks). Any object that you would like to have injected to your @Subject annotated object you have to annotate with @Collaborator. The main difference between the approaches is such that here in Spock you have to create mocks manually and in Mockito @Mock annotation does that for you.

See here to read more about it.