當沒有接口時、不可繼承時,若是使用mock方案進行單元測試

原版代碼:java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/*
    7.4 替換一個HTTP鏈接

    學習到如何爲沒有Java接口的類(即HttpURLConnection類)編寫mock。
 */

public class WebClient {
    public String getContent (URL url){
        StringBuffer content = new StringBuffer();
        try{
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setDoInput(true);
            InputStream in = connection.getInputStream();
            byte[] buffer = new byte[2048];
            int count;
            while (-1 != (count = in.read(buffer))) {
                content.append(new String(buffer,0,count));
            }
        } catch (IOException e) {
            return null;
        }
        return content.toString();
    }
}

 

採用方法工廠重構:sql

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/*
    第一次重構
 */

public class WebClient1 {
    public String getContent (URL url){
        StringBuffer content = new StringBuffer();
        try{
            HttpURLConnection connection = createHttpURLConnection(url);
            connection.setDoInput(true);
            InputStream in = connection.getInputStream();
            byte[] buffer = new byte[2048];
            int count;
            while (-1 != (count = in.read(buffer))) {
                content.append(new String(buffer,0,count));
            }
        } catch (IOException e) {
            return null;
        }
        return content.toString();
    }

    protected HttpURLConnection createHttpURLConnection(URL url) throws IOException {
        return (HttpURLConnection)url.openConnection();
    }
}

對其的測試:app

import org.junit.Test;

import java.net.HttpURLConnection;
import java.net.URL;

/*
public class TestWebClient1 {

    @Test
    public void testGetContentOk() throws Exception{
        MockHttpConnection mockHttpConnection = new MockHttpConnection();
        mockHttpConnection.setExpectedInputStream(new Byt
        、、、
    }

    private class TestableWebClient1 extends WebClient1{
        private HttpURLConnection connection;

        public void setHttpURLConnection(HttpURLConnection connection) {
            this.connection=connection;
        }

        public HttpURLConnection createHttpURLConnection(URL url) {
            return this.connection;
        }
    }
}
*/

 


採用類工廠重構:學習

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

interface ConnectionFactory{
    InputStream getData() throws Exception;
}

public class WebClient2 {
    public String getContent (ConnectionFactory factory){
        StringBuffer content = new StringBuffer();
        try{
            InputStream in = factory.getData();
            byte[] buffer = new byte[2048];
            int count;
            while (-1 != (count = in.read(buffer))) {
                content.append(new String(buffer,0,count));
            }
        } catch (IOException e) {
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }
}

對其的測試:測試

import org.junit.Test;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;

public class TestWebClient2 {

    @Test
    public void testGetContentOk(){

    }

    public class HttpURLConnectionFactory implements ConnectionFactory{
        private URL url;

        public HttpURLConnectionFactory(URL url) {
            this.url = url;
        }

        public InputStream getData() throws Exception {
            HttpURLConnection connection =
                    (HttpURLConnection)this.url.openConnection();
            return connection.getInputStream();
        }
    }
    public class MockURLConnectionFactory implements ConnectionFactory{
        private InputStream inputStream;

        public void setInputStream(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        public InputStream getData() throws Exception {
            return inputStream;
        }
    }
}
相關文章
相關標籤/搜索