Java Junit测试HTTP POST请求

14

我需要测试以下方法,但不能更改该方法本身。该方法向服务器发送一个POST请求,但我需要创建一个独立于服务器的测试用例。

以前我测试过类似的方法,将其重定向到一个本地文件。但是,在那种情况下,我将协议设置为文件,主机名设置为localhost,端口设置为-1。

我的问题是,该方法执行了一个POST请求并将其转换为HttpURLConnection,而wr = new DataOutputStream(conn.getOutputStream()); 在通过http访问本地txt文件时无法正常工作。

//构造函数

public HTTPConnector(String usr, String pwd, String protocol,
            String hostname, int port) {
        this.usr = usr;
        this.pwd = pwd;
        this.protocol = protocol;
        this.hostname = hostname;
        this.port = port;

        // connect();
    }

//我需要测试的方法

public String doPost(String reference, String data) throws IOException {
        URL url = null;
        HttpURLConnection conn = null;
        BufferedReader rd = null;
        DataOutputStream wr = null;
        InputStream is = null;
        String line = null;
        StringBuffer response = null;

        url = new URL(protocol, hostname, port, reference);
        conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("POST");

        conn.setRequestProperty("Authorization", "Basic dGVzdDphc2Rm");
        conn.setRequestProperty("Content-Type", "application/xml");

        conn.setUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);

        // Send response
        wr = new DataOutputStream(conn.getOutputStream());
        wr.writeBytes(data);
        wr.flush();
        wr.close();

        // Get response
        is = conn.getInputStream();
        rd = new BufferedReader(new InputStreamReader(is));
        response = new StringBuffer();
        while ((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();
    }

//我可以测试的方法

public String doGet(String reference) throws IOException {
        connect();
        URL url = new URL(protocol, hostname, port, reference);
        InputStream content = (InputStream) url.getContent();

        BufferedReader xml = new BufferedReader(new InputStreamReader(content));
        return xml.readLine();
    }

你考虑过使用模拟对象吗? - dapperwaterbuffalo
好吧,它没有进入任何本地内容,所以我很难找到重定向的方法。如果我能找到一种将其重定向到本地文件或类的方法,那么我可以想出一个解决方法,但我唯一能想到的方法是创建一个本地服务器/线程。但这意味着我必须将我的测试指向服务器,以在那里获取数据,并且服务器本身需要编程来处理请求,这比效率更高的工作量要大得多。 - Gabain1993
2个回答

7

这是一个样例测试。请注意,我所做的断言仅供演示目的,您需要根据自己的需求进行调整。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ toTest.class, URL.class, HttpURLConnection.class })
public class soTest {
    /**
     * test response.
     */
    private static final String TEST_RESPONSE = "test\nresponse";

    /**
     * test data.
     */
    private static final String DATA = RandomStringUtils.randomAscii(125);

    /**
     * test port.
     */
    private static final int PORT = 8080;

    /**
     * test hosts.
     */
    private static final String HOSTNAME = "hostname";

    /**
     * test protocol.
     */
    private static final String PROTOCOL = "http";

    /**
     * test reference.
     */
    private static final String REFERENCE = "REFERENCE";

    /**
     * URL mock.
     */
    private URL url;

    /**
     * HttpURLConnection mock.
     */
    private HttpURLConnection connection;

    /**
     * Our output.
     */
    private ByteArrayOutputStream output;

    /**
     * Our input.
     */
    private ByteArrayInputStream input;

    /**
     * Instance under tests.
     */
    private toTest instance;

    @Before
    public void setUp() throws Exception
    {
        this.url = PowerMockito.mock(URL.class);
        this.connection = PowerMockito.mock(HttpURLConnection.class);

        this.output = new ByteArrayOutputStream();
        this.input = new ByteArrayInputStream(TEST_RESPONSE.getBytes());
        this.instance = new toTest(PROTOCOL, HOSTNAME, PORT);

        PowerMockito.whenNew(URL.class).withArguments(PROTOCOL, HOSTNAME, PORT, REFERENCE).thenReturn(this.url);
    }

    @Test
    public void testDoPost() throws Exception
    {
        PowerMockito.doReturn(this.connection).when(this.url).openConnection();
        PowerMockito.doReturn(this.output).when(this.connection).getOutputStream();
        PowerMockito.doReturn(this.input).when(this.connection).getInputStream();

        final String response = this.instance.doPost(REFERENCE, DATA);

        PowerMockito.verifyNew(URL.class);
        new URL(PROTOCOL, HOSTNAME, PORT, REFERENCE);

        // Mockito.verify(this.url).openConnection(); // cannot be verified (mockito limitation) 
        Mockito.verify(this.connection).getOutputStream();
        Mockito.verify(this.connection).setRequestMethod("POST");
        Mockito.verify(this.connection).setRequestProperty("Authorization", "Basic dGVzdDphc2Rm");
        Mockito.verify(this.connection).setRequestProperty("Content-Type", "application/xml");
        Mockito.verify(this.connection).setUseCaches(false);
        Mockito.verify(this.connection).setDoInput(true);
        Mockito.verify(this.connection).setDoOutput(true);
        Mockito.verify(this.connection).getInputStream();

        assertArrayEquals(DATA.getBytes(), this.output.toByteArray());
        assertEquals(TEST_RESPONSE.replaceAll("\n", "\r") + "\r", response);
    }
}


@Data
public class toTest {
    private final String protocol, hostname;

    private final  int port;

    public String doPost(String reference, String data) throws IOException
    {
        // your method, not modified
    }
}

依赖项:

  • commons-lang 2.5
  • powermock-api-mockito 1.4.11
  • powermock-module-junit4 1.4.11
  • junit 4.10
  • 在测试类中使用 lombok 0.11.0

1
似乎是一个好的解决方案,但我遇到了一个错误,不知道如何解决,也许你可以帮忙解决一下:类型org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner无法解析。它是从所需的.class文件间接引用的。 - Gabain1993
这个类是由 powermock-module-junit4 提供的,这很奇怪。你应该检查一下你的类路径。 - user180100

6
如果您没有机会重构被测试的方法,那么一种新颖的方法是模拟它所使用的HttpUrlConnection。乍一看这似乎很困难,因为HttpUrlConnection并不是作为参数传递的。然而,您可以通过控制从url.openConnection返回的连接来控制它。
这由注册在传递给构造函数的协议的java.net.URL中的协议处理程序来管理。诀窍是注册一个新的协议处理程序(有关详情,请参见Java - 注册自定义URL协议处理程序)。
您的新协议处理程序应返回一个模拟的HttpUrlConnection,然后您可以在测试中使用它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接