JUnitテストの異常

JUnitテストの異常

  1. 古文
    @Test
    void name() {
        boolean hasException = false;
        String exceptionMessage = null;
        try {
            check();
        } catch (RuntimeException e) {
            hasException = true;
            exceptionMessage = e.getMessage();
        }
        assertEquals("runtime", exceptionMessage);
        assertTrue(hasException);
    }

    void check() {
        throw new RuntimeException("runtime");
    }
  1. (当てにならない)手書き
    チェックとメッセージ例外タイプ
   @Test
   void name() {
      assertThrows(RuntimeException.class, () -> check(), "aaa");
   }

   void check() {
       throw new RuntimeException("runtime");
   }

このテストは、我々はその異常なテストメッセージを見つけましたが、生きることができません。
PaのPaのソース

消費量は、実際にテストメッセージが珍しいニュースではありませんが、例外が期待されていない、と消費への障害がないことがわかりました。
2.1手書き

   @Test
    void name() {
        final RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> check());
        assertEquals("runtime", runtimeException.getMessage());
    }

    void check() {
        throw new RuntimeException("runtime");
    }

3.ストリーミング書き込み

     @Test
    void name() {
        assertThatThrownBy(() -> check())
            .isInstanceOf(RuntimeException.class)
            .hasMessage("runtime");
    }

    void check() {
        throw new RuntimeException("runtime");
    }

個人的に私は最もエレガントなの範囲内の電流の流れの文言を考えます。

おすすめ

転載: www.cnblogs.com/qulianqing/p/12622891.html