亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 250 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號