Mockito:实际上,与这个模拟的交互为零
实际上我尝试了不同的方法,但测试用例每次都失败,我不确定我做错了什么。
我浏览了所有类似的问题&尝试了所有的方法,但仍然无法得到大部分。
但是我仍然收到以下错误。我还附上了代码供参考。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论