【发布时间】:2016-04-12 11:11:47
【问题描述】:
我正在尝试在 Android Java 中测试套接字通信,但似乎无法模拟工作。
首先,使用 Mockito,mock(Socket.class) 会抛出异常 java.lang.VerifyError。
所以我像这样编写了我的模拟:
public void testMyTest(){
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
try{
byte[] buffer = new byte[6];
os.write("poulet".getBytes());
is.read(buffer, 0, 6);
Log.w(LOG_TAG, "Read result:" + (new String(buffer, "UTF-8")));
} catch(IOException e){}
}
但是,当我调用 os.write() 时,is 并没有从 os 读取。原始结果是[B@42204320,在字符串形式中,它看起来像������������。我尝试评论os.write(),但没有任何改变。
有人知道如何链接输入流以读取输出流吗?
为了测试我刚刚调用的课程
final Socket mockedSocket1 = new Socket();
final Socket mockedSocket2 = new Socket();
when(mockedSocket1.getInputStream()).thenReturn(is);
when(mockedSocket2.getOutputStream()).thenReturn(os)
这样我的类就可以获得我要测试的链接输出和输入流。
非常感谢!
【问题讨论】:
标签: java android unit-testing testing automated-tests