当没有接口时不可继承时,如果使用mock方案进行单元测试
Posted junjie2019
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当没有接口时不可继承时,如果使用mock方案进行单元测试相关的知识,希望对你有一定的参考价值。
原版代码:
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方案进行单元测试的主要内容,如果未能解决你的问题,请参考以下文章