Mockito:实际上,与这个模拟的交互为零

发布于 2025-01-16 14:30:06 字数 2831 浏览 2 评论 0原文

  • 实际上我尝试了不同的方法,但测试用例每次都失败,我不确定我做错了什么。

  • 我浏览了所有类似的问题&尝试了所有的方法,但仍然无法得到大部分。

  • 但是我仍然收到以下错误。我还附上了代码供参考。

    eventListener.onLogin();
    ->在com.plivo.endpoint.login.TEndpointLoginTest.t2_endpoint_login_success_test(TEndpointLoginTest.java:65)
    
    事实上,与这个模拟的互动为零。
    
    

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();
}
  • 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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文