Junit 断言失败的 HTTP 发布请求

我对我的 spring boot 应用程序进行了 HTTPClient 测试。如果对服务器的 POST 请求是 2048 字节或以上的字符串,我有一个类会抛出异常。


@Component

 public class ApplicationRequestSizeLimitFilter extends OncePerRequestFilter {


    @Override

    protected void doFilterInternal(HttpServletRequest request,

            HttpServletResponse response, FilterChain filterChain)

                    throws ServletException, IOException {

        System.out.println(request.getContentLength());

        if (request.getContentLengthLong() >= 2048) {

            throw new IOException("Request content exceeded limit of 2048 bytes");

        }

        filterChain.doFilter(request, response);


    }



}

我为它创建了一个单元测试,但我不确定如何编写断言语句来检查它是否无法发布请求。


到目前为止,我的测试课上有这个


@Test

    public void testSize() throws ClientProtocolException, IOException {

        Random r = new Random(123);

        long start = System.currentTimeMillis();

        String s = "";

        for (int i = 0; i < 65536; i++)

            s += r.nextInt(2);

        String result = Request.Post(mockAddress)

                .connectTimeout(2000)

                .socketTimeout(2000)

                .bodyString(s, ContentType.TEXT_PLAIN)

                .execute().returnContent().asString();


    }

该测试失败了,这正是我想要的,但我想创建一个断言以使其通过(断言由于超过字节限制而导致 http 响应失败)。


富国沪深
浏览 166回答 4
4回答

子衿沉夜

您可以用 try/catch 包围失败的部分,并在 try 块的末尾调用 fail()。如果抛出异常,则不应到达 fail() 指令,并且您的测试应该通过。

慕无忌1623718

@Test有一个参数断言一个特定的异常被抛出,你可以像这样编写你的测试:&nbsp; &nbsp; @Test(expected = IOException.class)&nbsp; &nbsp; public void testSize() throws ClientProtocolException, IOException {&nbsp; &nbsp; ...&nbsp; &nbsp; }

暮色呼如

您可以通过 3 种方式实现这一目标:1)@Test(expected = ....在提供要检查的异常类的地方使用 ) 注释。@Test(expected = IOException.class)public void test() {&nbsp; //... your test logic}这不是异常测试的推荐方法,除非您的测试真的很小并且只做一件事。否则,您可能会抛出异常IOException,但您无法确定是测试代码的哪一部分引起的。2)@Rule对类使用注解ExpectedException:@Rulepublic ExpectedException exceptionRule = ExpectedException.none();@Testpublic void testExpectedException() {&nbsp; &nbsp; exceptionRule.expect(IOException.class);&nbsp; &nbsp; exceptionRule.expectMessage("Request too big.");//... rest of your test logic here}请注意,exceptionRule必须是public。3)最后一个,很老式的方式:@Testpublic void test() {&nbsp; try {&nbsp; &nbsp; // your test logic&nbsp; &nbsp; fail(); // if we get to that point it means that exception was not thrown, therefore test should fail.&nbsp; } catch (IOException e) {&nbsp; &nbsp; // if we get here, test is successfull and code seems to be ok.&nbsp; }}这是一种老式的方法,它会在本应干净的测试中添加一些不必要的代码。

宝慕林4294392

还有另一种解决方案,尚未在这些答案中提出,这是我个人的偏好。assertThatThrownBy 断言在你的情况下@Testpublic void testSizeException(){&nbsp; assertThatThrownBy(()-> Request.Post(mockAddress)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .connectTimeout(2000)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .socketTimeout(2000)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .bodyString(s, ContentType.TEXT_PLAIN)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .execute().returnContent().asString())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .isInstanceOf(IOException.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .hasMessageContaining("Request content exceeded limit of 2048&nbsp;&nbsp; bytes");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java