久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

測試 Java 套接字

Testing Java Sockets(測試 Java 套接字)
本文介紹了測試 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 套接字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 国产精品一区二区av | 性高湖久久久久久久久3小时 | 欧美国产精品久久久 | 羞羞涩涩在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 亚洲三级在线观看 | 99亚洲精品 | 亚洲永久字幕 | 亚洲免费高清 | www.一级片 | 99久久久久 | 青青草华人在线视频 | 国产丝袜一区二区三区免费视频 | 无码一区二区三区视频 | 成人h免费观看视频 | 一区二区三区在线免费看 | 国产视频一视频二 | 999视频| 欧美日韩精品久久久免费观看 | 亚洲人成在线观看 | 亚洲一本| 日韩av成人| 一区不卡在线观看 | 毛片在线免费播放 | 成人欧美一区二区三区黑人孕妇 | 精品国产伦一区二区三区观看体验 | 国产成人精品网站 | 欧美精品成人影院 | 日干夜操 | 国产精品av久久久久久久久久 | 一区二区在线 | 国产欧美日韩在线一区 | 日韩欧美在线一区 | 亚洲一区二区在线播放 | 一区二区三区精品 | 国产片网站 | 久久久精品高清 | 日韩精品一区二区三区视频播放 | 国产精品成人一区二区三区夜夜夜 | 综合国产在线 | 黄色片在线免费看 |