问题描述
我正在开发一个网络应用程序,我想正确进行单元测试.这次我们会做的,你知道吗?:)
I'm developing a network application and I want to get unit testing right. THIS time we'll do it, you know? :)
不过,我在测试网络连接时遇到了麻烦.
I'm have trouble testing network connections, though.
在我的应用程序中,我使用普通的 java.net.Sockets.
In my application I use plain java.net.Sockets.
例如:
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Message {
byte[] payload;
public Message(byte[] payload) {
this.payload = payload;
}
public boolean sendTo(String hostname, int port) {
boolean sent = false;
try {
Socket socket = new Socket(hostname, port);
OutputStream out = socket.getOutputStream();
out.write(payload);
socket.close();
sent = true;
} catch (UnknownHostException e) {
} catch (IOException e) {
}
return sent;
}
}
我阅读了关于模拟的文章,但不知道如何应用它.
I read about mocking but am not sure how to apply it.
推荐答案
如果我要测试代码,我会执行以下操作.
If I was to test the code, I'd do the following.
首先,重构代码,使 Socket 不会在您要测试的方法中直接实例化.下面的示例显示了我能想到的最小更改.未来的更改可能会将 Socket 创建分解为一个完全独立的类,但我喜欢小步骤,我不喜欢对未经测试的代码进行大的更改.
Firstly, refactor the code so that the Socket isn't directly instantiated in the method you want to test. The example below shows the smallest change I can think of to make that happen. Future changes might factor out the Socket creation to a completely separate class, but I like small steps and I don't like making big changes on untested code.
public boolean sendTo(String hostname, int port) {
boolean sent = false;
try {
Socket socket = createSocket();
OutputStream out = socket.getOutputStream();
out.write(payload);
socket.close();
sent = true;
} catch (UnknownHostException e) {
// TODO
} catch (IOException e) {
// TODO
}
return sent;
}
protected Socket createSocket() {
return new Socket();
}
现在套接字创建逻辑在您尝试测试的方法之外,您可以开始模拟事物并挂钩到套接字的创建.
Now that the socket creation logic is outside of the method you are trying to test, you can start to mock things up and hook into the creation the socket.
public class MessageTest {
@Test
public void testSimplePayload() () {
byte[] emptyPayload = new byte[1001];
// Using Mockito
final Socket socket = mock(Socket.class);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
when(socket.getOutputStream()).thenReturn(byteArrayOutputStream);
Message text = new Message(emptyPayload) {
@Override
protected Socket createSocket() {
return socket;
}
};
Assert.assertTrue("Message sent successfully", text.sendTo("localhost", "1234"));
Assert.assertEquals("whatever you wanted to send".getBytes(), byteArrayOutputStream.toByteArray());
}
}
在您要测试的单元上覆盖单个方法对于测试非常有用,尤其是在具有可怕依赖关系的丑陋代码中.显然最好的解决方案是整理依赖关系(在这种情况下,我认为 Message 不依赖于 Socket,也许有一个 Messagergloomcoder 建议的界面),但是以尽可能小的步骤向解决方案迈进是很好的.
Overriding individual methods on units you want to test is really useful for testing, especially in ugly code with horrible dependencies. Obviously the best solution is sorting out dependencies (in this case I would think that a Message not depend on a Socket, maybe there is a Messager interface as glowcoder suggests), but it's nice to move towards the solution in the smallest possible steps.
这篇关于测试 Java 套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)