我如何编写测试驱动的java类下载管理器



我正在开发一个下载管理器使用UP设计。在这个精化迭代中,我的主要用例是:下载文件。这是Download.java

public class Download  implements Runnable {
// Max size of download buffer.
private static final int MAX_BUFFER_SIZE = 1024;
// These are the status names.
public static final String STATUSES[] = {"Downloading", "Complete"};
// These are the status codes.
public static final int DOWNLOADING = 0;
public static final int COMPLETE = 1;
public static void main(String[] args) throws MalformedURLException {
    System.out.println("Welcome to Download Manager.");
    System.out.println("Enter a URL.");
    URL url;
    String s;
    Scanner scan= new Scanner(System.in);
    s=scan.nextLine();
    url= new URL(s);
    Download download=new Download(url);
}
private URL url; // download URL
private int size; // size of download in bytes
private int downloaded; // number of bytes downloaded
private int status; // current status of download
// Constructor for Download.
public Download(URL url) {
    this.url = url;
    size = -1;
    downloaded = 0;
    status = DOWNLOADING;
    // Begin the download.
    download();
}
 // Start or resume downloading.
private void download() {
    System.out.println("Starting.");
    Thread thread = new Thread(this);
    thread.start();
}
// Download file.
public void run() {
    RandomAccessFile file = null;
    InputStream stream = null;
    try {
        // Open connection to URL.
        HttpURLConnection connection =
                (HttpURLConnection) url.openConnection();
        // Specify what portion of file to download.
        connection.setRequestProperty("Range",
                "bytes=" + downloaded + "-");
        // Connect to server.
        connection.connect();

        int contentLength = connection.getContentLength();

  /* Set the size for this download if it
     hasn't been already set. */
        if (size == -1) {
            size = contentLength;
        }
        // Open file and seek to the end of it.
        file = new RandomAccessFile(getFileName(url), "rw");
        file.seek(downloaded);
        stream = connection.getInputStream();
        while (status == DOWNLOADING) {
    /* Size buffer according to how much of the
       file is left to download. */
            byte buffer[];
            if (size - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[size - downloaded];
            }
            System.out.print("%"+(downloaded/size)+'r');
            // Read from server into buffer.
            int read = stream.read(buffer);
            if (read == -1){
                System.out.println("File was downloaded");
                break;
            }
            // Write buffer to file.
            file.write(buffer, 0, read);
            downloaded += read;
        }
  /* Change status to complete if this point was
     reached because downloading has finished. */
        if (status == DOWNLOADING) {
            status = COMPLETE;
        }
    } catch (Exception e) {
       System.out.println("Error!");
    } finally {
        // Close file.
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {}
        }
        // Close connection to server.
        if (stream != null) {
            try {
                stream.close();
            } catch (Exception e) {}
        }
    }
}

如何编写这段代码的测试代码?例如,我应该测试URL验证,还是应该控制文件是否正在下载?我怎么做这些检查?谢谢。

您编写的类很难测试,因为它试图做的事情太多了。如果您要将一些职责提取到外部依赖项中,那么您最终可能会得到一个类来管理来自url的低级下载,而另一个类来管理本地文件系统。比如:

interface UrlDownloader {
    InputStream download(URL url, int offset) throws IOException;
}
interface DownloadFolder {
    List<String> getFiles();
    void writeToFile(String filename, InputStream contents) throws IOException;
    void getFileSize(String filename);
}
然后可以使用这些类的模拟版本测试下载管理器。使用像mockito这样的库,您可以编写这样的测试:
@Test
public void canDownloadCompleteFile() throws IOException {
    URL url = new URL("http://example.com/file.txt");
    InputStream inputStream = new ByteArrayInputStream("abc".getBytes());
    UrlDownloader urlDownloader = mock(UrlDownloader.class);
    DownloadFolder downloadFolder = mock(DownloadFolder.class);
    when(urlDownloader.download(url, 0)).thenReturn(inputStream);
    DownloadManager manager = new DownloadManager(urlDownloader, downloadFolder);
    manager.download(url);
    verify(downloadFolder).writeToFile("file.txt", inputStream);
}

使用mockito,您可以控制依赖项何时抛出异常,或者验证使用特定参数调用方法。

另一种方法是创建伪类来实现您的接口,并使用内存中的数据结构而不是真正的文件系统/网络。然后可以使用assertEquals等来测试这些类的状态:

@Test
public void canDownloadCompleteFile() throws IOException {
    URL url = new URL("http://example.com/file.txt");
    FakeDownloadFolder downloadFolder = new FakeDownloadFolder();
    FakeUrlDownloader urlDownloader = new FakeUrlDownloader();
    urlDownloader.setUrlContents(url, "abc".getBytes());
    DownloadManager manager = new DownloadManager(urlDownloader, downloadFolder);
    manager.download(url);
    assertEquals("abc".getBytes(), downloadFolder.getFileAsByteArray("file.txt"));
}

测试驱动开发背后的思想是,您只编写由测试支持的代码。所以你需要实现足够的DownloadManager来通过第一个测试,然后添加另一个测试(例如,恢复未完成的下载)。

回答你的直接问题:为这些代码写一个测试真的很难。

单元测试的美妙之处在于,它们向您展示了代码的客户端使用模块是多么容易。有一个很大的领域叫做"面向对象的分析与设计",旨在帮助程序员解决这些问题。

在这里,你应该改变你的代码,有几个例程具有单一的责任,例如,一个用于网络通信,一个用于存储自定义数据流到硬盘,另一个用于处理使用输入。这样你就可以单独测试这些"例程",甚至提供你自己的"网络"环境(例如,类将放置硬编码值而不是真正连接到网络)

相关内容

  • 没有找到相关文章

最新更新