繁体   English   中英

对URLConnection进行JUnit测试,使用EasyMock?

[英]JUnit test on URLConnection, use EasyMock?

嘿,在最后一天左右一直试图解决这个问题但是碰到了砖墙。 试图对这段代码进行单元测试。 但不确定是否需要使用EasyMock? 在网上看似很少的例子,但似乎是使用旧技术。

public boolean verifyConnection(final String url) {
    boolean result;

    final int timeout = getConnectionTimeout();
    if (timeout < 0) {
        log.info("No need to verify connection to client. Supplied timeout = {}", timeout);
        result = true;
    } else {
        try {
            log.debug("URL: {} Timeout: {} ", url, timeout);

            final URL targetUrl = new URL(url);
            final HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection();

            connection.setConnectTimeout(timeout);
            connection.connect();
            result = true;
        } catch (ConnectException e) {
            log.warn("Could not connect to client supplied url: " + url, e);
            result = false;
        } catch (MalformedURLException e) {
            log.error("Malformed client supplied url: " + url, e);
            result = false;
        } catch (IOException e) {
            log.warn("Could not connect to client supplied url: " + url, e);
            result = false;
        }
    }
    return result;
}

只需在URL中检查其有效并返回T或F.

如果你想模拟这个方法,我建议传入URL而不是String。 没有你的方法创建它需要的URL; 让客户为您创建URL并将其传入。这样,如果需要,您的测试可以替换模拟。

它几乎是一个依赖注入的想法 - 你的方法应该给它的依赖,而不是自己创建它们。 对“新”的呼唤是死亡的赠品。

这不是一个剧烈的变化。 您可以重载该方法并具有两个签名:一个接受URL字符串,另一个接受URL本身。 让第一种方法创建URL并调用第二种方法。 这样你就可以测试它,并且为了方便,仍然在你的API中使用带有String签名的方法。

我一直观察到可以尽可能地避免模拟,因为它可能导致难以维护JUnit测试并且无法完成整个目的。

我的建议是从JUnit本身在本地机器上创建一个临时服务器。 在JUnit的开头,您可以使用Java套接字创建一个服务器(不超过10-15行编码),然后在您的代码中传递本地服务器的URL。 这样您就可以减少模拟并确保最大程度的代码覆盖率。

像这样的东西 -

public class SimpleServer extends Thread {

public void run() {
    try {
        serverSocket = new ServerSocket(port);

          while (true) {
            Socket s = serverSocket.accept(); 
          }
    } 
    catch (IOException e) {
            e.printStackTrace();
    }
    finally {
        serverSocket = null;
    }
}
}

试图设置HttpURLConnection的模拟实现。 喜欢

public class MockHttpURLConnection extends HttpURLConnection { '

然后在类中添加方法来覆盖

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

所以测试看起来像这样:

@Test
public void testGetContentOk() throws Exception
{
    String url = "http://localhost";

    MockHttpURLConnection mockConnection = new MockHttpURLConnection();

    TestableWebClient client = new TestableWebClient();
    client.setHttpURLConnection(mockConnection);

    boolean result = client.verify(url);

    assertEquals(true, result);
}

@Test
public void testDoesNotGetContentOk() throws Exception
{
    String url = "http://1.2.3.4";

    MockHttpURLConnection mockConnection = new MockHttpURLConnection();

    TestableWebClient client = new TestableWebClient();
    client.setHttpURLConnection(mockConnection);

    boolean result = client.verify(url);

    assertEquals(false, result);
}

/**
 * An inner, private class that extends WebClient and allows us
 * to override the createHttpURLConnection method.
 */
private class TestableWebClient extends WebClient1 {

    private HttpURLConnection connection;

    /**
     * Setter method for the HttpURLConnection.
     *
     * @param connection
     */
    public void setHttpURLConnection(HttpURLConnection connection)
    {
        this.connection = connection;
    }

    /**
     * A method that we overwrite to create the URL connection.
     */
    @Override
    public HttpURLConnection createHttpURLConnection(URL url) throws IOException
    {
        return this.connection;
    }
}

第一部分通过但虚假测试得到了真实,感谢回馈到目前为止最好的网站,我找到了帮助。 如果想想正确的话,请告诉我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM