'Mockito : Actually, there were zero interactions with this mock

  • Actually I tried different ways but test case is getting failed every time, i'm not sure what i'm doing wrong.

  • I look through all the similar questions & tried all the ways but still not able to get most of it.

  • However I still get the below error. I have also attached code for reference.

    eventListener.onLogin();
    -> at com.plivo.endpoint.login.TEndpointLoginTest.t2_endpoint_login_success_test(TEndpointLoginTest.java:65)
    
    Actually, there were zero interactions with this mock.
    
    

TEndpointLoginTest.java

@RunWith(MockitoJUnitRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TEndpointLoginTest {

    @Mock
    private TEventListener eventListener;

    private TEndpoint endpoint;

    @BeforeClass
    public static void create() {
    }

    @Before
    public void setUp() {
        MockitoAnnotations.openMocks(this);
        if (endpoint == null) {
            endpoint = TEndpoint.newInstance(eventListener);
        }
    }

    @Test
    public void t1_endpoint_isInitialized_test() {
        assertThat(endpoint).isNotNull();
    }

    // Login
    @Test
    public void t2_endpoint_login_success_test() {
        endpoint.login(1);
        verify(eventListener, timeout(1200)).onLogin();
    }

}

TEndpoint.java

public class TEndpoint {
    protected TEventListener eventListener;
    private TSipController sipController;

    public TEndpoint(TEventListener listener) {
        this.eventListener = listener;
        initLib();
    }

    public static TEndpoint newInstance(TEventListener eventListener) {
        TEndpoint endpoint = new TEndpoint(eventListener);
        return endpoint;
    }

    private void initLib() {
        sipController = TSipController.getInstance(this);
    }

    public void login(int flag) {
        if (flag == 1) {
            sipController.fireLogin();
        } else {
            sipController.fireLogOut();
        }
    }
}

TSipController.java

public class TSipController {
    private static TSipController controller;
    private final TEventListener eventListener;
    private final TEndpoint endpoint;

    public static TSipController getInstance(TEndpoint endpoint) {
        if (controller == null) {
            controller = new TSipController(endpoint);
        }
        return controller;
    }

    private TSipController(TEndpoint endpoint) {
        this.eventListener = endpoint.eventListener;
        this.endpoint = endpoint;
    }

    public void fireLogin(){
        new Handler(Looper.getMainLooper()).postDelayed(eventListener::onLogin, 1000);
    }

    public void fireLogOut(){
        new Handler(Looper.getMainLooper()).postDelayed(eventListener::onLogOut, 1000);
    }
}

TEventListener.java

public interface TEventListener {
    void onLogin();
    void onLogOut();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source