LCOV - code coverage report
Current view: top level - testing - GerritJUnit.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 11 11 100.0 %
Date: 2022-11-19 15:00:39 Functions: 1 1 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2019 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.testing;
      16             : 
      17             : /** Static JUnit utility methods. */
      18             : public class GerritJUnit {
      19             :   /**
      20             :    * Assert that an exception is thrown by a block of code.
      21             :    *
      22             :    * <p>This method is source-compatible with <a
      23             :    * href="https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThrows(java.lang.Class,%20org.junit.function.ThrowingRunnable)">JUnit
      24             :    * 4.13 beta</a>.
      25             :    *
      26             :    * <p>This construction is recommended by the Truth team for use in conjunction with asserting
      27             :    * over a {@code ThrowableSubject} on the return type:
      28             :    *
      29             :    * <pre>{@code
      30             :    * MyException e = assertThrows(MyException.class, () -> doSomething(foo));
      31             :    * assertThat(e).isInstanceOf(MySubException.class);
      32             :    * assertThat(e).hasMessageThat().contains("sub-exception occurred");
      33             :    * }</pre>
      34             :    *
      35             :    * @param throwableClass expected exception type.
      36             :    * @param runnable runnable containing arbitrary code.
      37             :    * @return exception that was thrown.
      38             :    */
      39             :   public static <T extends Throwable> T assertThrows(
      40             :       Class<T> throwableClass, ThrowingRunnable runnable) {
      41             :     try {
      42           1 :       runnable.run();
      43         107 :     } catch (Throwable t) {
      44         107 :       if (!throwableClass.isInstance(t)) {
      45           1 :         throw new AssertionError(
      46             :             "expected "
      47           1 :                 + throwableClass.getName()
      48             :                 + " but "
      49           1 :                 + t.getClass().getName()
      50             :                 + " was thrown",
      51             :             t);
      52             :       }
      53             :       @SuppressWarnings("unchecked")
      54         107 :       T toReturn = (T) t;
      55         107 :       return toReturn;
      56           1 :     }
      57           1 :     throw new AssertionError(
      58           1 :         "expected " + throwableClass.getName() + " but no exception was thrown");
      59             :   }
      60             : 
      61             :   @FunctionalInterface
      62             :   public interface ThrowingRunnable {
      63             :     void run() throws Throwable;
      64             :   }
      65             : 
      66             :   private GerritJUnit() {}
      67             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750