测试 Java 套接字

时间:2022-11-01
本文介绍了测试 Java 套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在开发一个网络应用程序,我想正确进行单元测试.这次我们会做的,你知道吗?:)

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 套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:模拟实例在@Mock 注释后为空 下一条:在 Java 中将 WireMock 与 SOAP Web 服务一起使用

相关文章

最新文章