Java_UT_Mock系列之-11Mockito与异步方法01

测试场景

在最近的测试中,涉及到了异步方法的单元测试。百度了一下之后,基本上的案例都来自于这里:

https://fernandocejas.com/2014/04/08/unit-testing-asynchronous-methods-with-mockito/

对比之后,发觉和我们的场景略微有些差异。我们的场景是:
某个client是一个通信客户端, 而在client内部维护了一个Connector类的实例来真正地完成Socket层面的send/recv等消息的发送和接收的工作。另外,有一个IListener接口来处理收到的消息,当Connector收到消息时,将调用IListener的onMessage方法来处理消息。 这样就完成了消息的异步处理。
被测的方法 client.login()的处理逻辑是:
1)通过connector.login来发送消息。
2)监控client.getConnected()的状态,检查是否登陆成功。
当底层(通信层)收到消息时,会通过connector.onMessage(data)来进行处理,即通知IListener,进而将登陆状态设置为登陆成功。
因此,在测试过程中,我们只需要Mock Connector,并在connector.login被调用时,通过answer 来调用
connector.onMessage(data),模拟来自对端的消息。

测试代码

具体的代码如下:
1 测试用例

import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.mockito.ArgumentCaptor;import org.mockito.Captor;import org.mockito.MockitoAnnotations;import org.mockito.Spy;import static org.mockito.Mockito.*;import org.mockito.invocation.InvocationOnMock;import org.mockito.stubbing.Answer;import demo.org.powermock.async.Client;import demo.org.powermock.async.Connector;import demo.org.powermock.async.IListener;
public class TestClient { 
String data="SUCCESS";
@Spyprivate Connector connector; 
 @Before public void initMocks() { 
MockitoAnnotations.initMocks(this); }
@Testpublic void TestClientLogin() {
Client client=new Client(connector,"localhost"); 
Answeranswer = new Answer() {
    @Override
public Object answer(InvocationOnMock invocation) throws Throwable {
    connector.onMessage(data);
    return null;
}
    };
doAnswer(answer).when(connector).login("localhost");
client.login();
Assert.assertTrue(client.getConnected());
}

}

2 被mock的底层通信类

public class Connector {
private IListener listener;
public void onMessage(String data) {
listener.onMessage(data);
}
public void setListener(IListener listener) {
this.listener=listener;
}
public void login(String url) {
System.out.println("sending login request to "+url);
}
}

3 回调具体类

public class Listener implements IListener {
private Client client;
public Listener(Client client) {
this.client=client;
}
@Override
public void onMessage(String data) {
System.out.println(data);
if(data.contains("SUCCESS")) {
client.setConnected(true);
}
};
}

4 回调接口

public interface IListener {
public void onMessage(String data);
}

5 被测类

public class Client {
private Connector connector;
private String server;
boolean connected=false;
public Client(Connector connector,String server) {
this.connector=connector;
this.server=server;
this.connector.setListener(new Listener(this));
}
public void setConnected(boolean connected) {
this.connected=connected;
}
public boolean getConnected() {
return connected;
}
public void login() {
int times=0;
// sending login request to remote
connector.login(server);
// waiting for response from network
while(!getConnected() && times <10) {
try {
Thread.sleep(100);
times++;
} catch (InterruptedException e) {
e.printStackTrace();
}}}}

你可能感兴趣的:(Java_UT_Mock系列之-11Mockito与异步方法01)