码迷,mamicode.com
首页 > 其他好文 > 详细

当没有接口时、不可继承时,如果使用mock方案进行单元测试

时间:2019-03-29 20:44:51      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:expected   set   puts   接口   tin   junit   read   nbsp   client   

原版代码:

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();
    }
}

 

采用方法工厂重构:

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();
    }
}

对其的测试:

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;
        }
    }
}

 

当没有接口时、不可继承时,如果使用mock方案进行单元测试

标签:expected   set   puts   接口   tin   junit   read   nbsp   client   

原文地址:https://www.cnblogs.com/junjie2019/p/10623584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!