1 package com.uber.nullaway; 2 3 import static org.hamcrest.CoreMatchers.instanceOf; 4 import static org.junit.Assert.assertEquals; 5 import static org.junit.Assert.assertThat; 6 import static org.junit.jupiter.api.Assertions.assertThrows; 7 8 import java.lang.reflect.InvocationTargetException; 9 import java.lang.reflect.Method; 10 import java.util.Arrays; 11 import org.junit.Before; 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 import org.junit.runners.JUnit4; 15 16 @RunWith(JUnit4.class) 17 public class DummyOptionsConfigTest { 18 19 DummyOptionsConfig dummyOptionsConfig; 20 21 @Before setup()22 public void setup() { 23 dummyOptionsConfig = new DummyOptionsConfig(); 24 } 25 26 @Test allDeclaredMethodsThrowIllegalStateException()27 public void allDeclaredMethodsThrowIllegalStateException() { 28 // DummyOptionsConfig is expected to throw a runtime exception if ever used (see documentation 29 // on that class) 30 // this test guarantees that all methods declared in the class throw the exception 31 Class<? extends DummyOptionsConfig> klass = dummyOptionsConfig.getClass(); 32 for (Method method : klass.getDeclaredMethods()) { 33 if (method.getName().contains("jacocoInit")) { 34 // Declared method added by jacoco coverage reporting (via reflection?). Plots within 35 // plots... 36 continue; 37 } 38 Class<?>[] parameterTypes = method.getParameterTypes(); 39 Object[] nullParams = Arrays.stream(parameterTypes).map((t) -> null).toArray(); 40 Exception reflectionException = 41 assertThrows( 42 InvocationTargetException.class, 43 () -> { 44 method.invoke(dummyOptionsConfig, nullParams); 45 }, 46 String.format( 47 "Expected method DummyOptionsConfig.%s to fail with IllegalStateException.", 48 method.getName())); 49 // The real exception, not wrapped by reflection exceptions 50 Throwable cause = reflectionException.getCause(); 51 assertThat(cause, instanceOf(IllegalStateException.class)); 52 IllegalStateException exception = (IllegalStateException) cause; 53 assertEquals(exception.getMessage(), DummyOptionsConfig.ERROR_MESSAGE); 54 } 55 } 56 } 57