Focus on Mock Object(2)

2008-04-09 04:06:00来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

A more complex environment: web application based on a Web Server. Here we compare servious ways to simulate the Web Server Condition with Mock Object.


The original WebClient source:

src: http://blog.chinaunix.net/resserver.php?blogId=8755&resource=junit72.txt

As you can see java.net.HttpURLConnection and java.net.URL are related to the Web Server wanted to mock.

Someone may think directly: just mock it with its subclass as MockHttpURLConnetion and MockURL.

public void testGetContentOk() throws Exception {
MockHttpURLConnection mockConnection = new MockHttpURLConnection();
mockConnection.setupGetInputStream(new ByteArrayInputStream("It works".getBytes()));
MockURL mockURL = new MockURL();

mockURL.setupOpenConnection(mockConnection);

WebClient client = new WebClient();
String result = client.getContent(mockURL);
assertEquals("It works", result);
}

But one thing they forgot: the java.net.URL is a final class, which meants it couldn't be extended. Then, How can we mock it besides this way?

Now, we find we must do something to the class java.net.URL, let us easily to mock it.

improved src: http://blog.chinaunix.net/resserver.php?blogId=8755&resource=junit73.txt

You can find that with this alternaive we can mock it easiler.

private class TestableWebClient extends WebClient {
private HttpURLConnection connection;
public void setHttpURLConnection(HttpURLConnection connection) {
this.connection = connection;
}
public HttpURLConnection createHttpURLConnection(URL url) throws IOException {
return this.connection;
}
}

As you can see, with the class TestableWebClient extended from WebClient, you can set the HttpURLConnection yourself and artfully avoid the URL problem.

Test Case src with the improvement: http://blog.chinaunix.net/resserver.php?blogId=8755&resource=junit74.txt

"This is a common refactoring approach called Method Factory refactoring, which is especially useful when the class to mock has no interface. The strategy is to extend that class, add some setter methods to control it, and override some of its getter methods to return what you want for the test."

But what are you truely test? The subclass which behaviors have been changed, not the original one.

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Focus on Mock Object(1)

下一篇:向水晶报表数据源存储过程传参数