Skip to content

Package: AssertThrows

AssertThrows

nameinstructionbranchcomplexitylinemethod
assertThrows(Class, Executable)
M: 36 C: 7
16%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 7 C: 3
30%
M: 0 C: 1
100%

Coverage

1: package util.testutilities;
2:
3: /**
4: * Assert against exceptions that get thrown.
5: */
6: public final class AssertThrows {
7:         
8:         /**
9:          * statische Funktion zum Testen, ob eine Exception geworfen wird.
10:          *
11:          * @param expectedType
12:          * erwarteter ExceptionType (Angabe in Form von bspw. ArithmeticException.class).
13:          * @param executable
14:          * auszuführender Code, in welchem die Exception erwartet wird (Angabe in Form von () -> {doit();} ).
15:          * @param <T>
16:          * bin mir nicht so sicher, was ich hier schreiben soll.
17:          * @return die geworfene Exception, falls sie mit dem expectedType übereinstimmt.
18:          */
19:         @SuppressWarnings("unchecked")
20:         public static <T extends Exception> T assertThrows(final Class<T> expectedType, final Executable executable) {
21:                 try {
22:                         executable.execute();
23:                 } catch (final Exception actualException) {
24:•                        if (expectedType.isInstance(actualException)) {
25:                                 return (T) actualException;
26:                         } else {
27:                                 throw new AssertionError(String.format(
28:                                                 "Unexpected exception type thrown, expected: %s, actual: %s",
29:                                                 expectedType.getName(),
30:                                                 actualException.getClass().getName()), actualException);
31:                         }
32:                 }
33:                 throw new AssertionError(
34:                                 String.format("Expected %s to be thrown, but nothing was thrown.", expectedType.getSimpleName()));
35:         }
36:         
37:         private AssertThrows() {
38:         }
39:
40: }