How to mock @Inject Api-Class with Mockito

I’m trying to test a Java method with Junit,

Unfortunately I can’t get any further at one point because the Api class was defended as @Inject

I actually tried everything I could, unfortunately null is always returned and the test fails every time.

    private MemberAPi memberApi;

    NewMember newMember = new NewMember();
    newMember = MemberApi.addMember(new CreateMemberParameterObj (newMember, getId , false, Obj ))

Test: I try to mock it like that e.g.

    private MemberAPi mockedMemberApi;



Mock the MemberAPI and the NewMember classes. Use @InjectMocks and Mockito will automatically inject the mockMemberAPI object.

Here is some code:

private Blam classToTest; // your class.

private MemberAPi mockMemberAPi;

private NewMember mockNewMember;

public void before()


I use the doReturn().when().xxx(); pattern instead of the when(mockedMemberAPi.addMember(anyObject())).thenReturn(mockMemberAPI); pattern.

Note: thenReturn(anyObject()); makes no sense because you can’t return anyObject().