为了账号安全,请及时绑定邮箱和手机立即绑定

Junit 断言失败的 HTTP 发布请求

Junit 断言失败的 HTTP 发布请求

富国沪深 2023-06-04 11:12:42
我对我的 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 响应失败)。
查看完整描述

4 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

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



查看完整回答
反对 回复 2023-06-04
?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

@Test有一个参数断言一个特定的异常被抛出,你可以像这样编写你的测试:


    @Test(expected = IOException.class)

    public void testSize() throws ClientProtocolException, IOException {

    ...

    }


查看完整回答
反对 回复 2023-06-04
?
暮色呼如

TA贡献1853条经验 获得超9个赞

您可以通过 3 种方式实现这一目标:


1)@Test(expected = ....在提供要检查的异常类的地方使用 ) 注释。


@Test(expected = IOException.class)

public void test() {

  //... your test logic

}

这不是异常测试的推荐方法,除非您的测试真的很小并且只做一件事。否则,您可能会抛出异常IOException,但您无法确定是测试代码的哪一部分引起的。


2)@Rule对类使用注解ExpectedException:


@Rule

public ExpectedException exceptionRule = ExpectedException.none();


@Test

public void testExpectedException() {

    exceptionRule.expect(IOException.class);

    exceptionRule.expectMessage("Request too big.");

//... rest of your test logic here

}

请注意,exceptionRule必须是public。


3)最后一个,很老式的方式:


@Test

public void test() {

  try {

    // your test logic

    fail(); // if we get to that point it means that exception was not thrown, therefore test should fail.

  } catch (IOException e) {

    // if we get here, test is successfull and code seems to be ok.

  }

}

这是一种老式的方法,它会在本应干净的测试中添加一些不必要的代码。


查看完整回答
反对 回复 2023-06-04
?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

还有另一种解决方案,尚未在这些答案中提出,这是我个人的偏好。assertThatThrownBy 断言


在你的情况下


@Test

public void testSizeException(){

  assertThatThrownBy(()-> Request.Post(mockAddress)

                  .connectTimeout(2000)

                  .socketTimeout(2000)

                  .bodyString(s, ContentType.TEXT_PLAIN)

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

                  .isInstanceOf(IOException.class)

                  .hasMessageContaining("Request content exceeded limit of 2048 

  bytes");

}


查看完整回答
反对 回复 2023-06-04
  • 4 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信