### Question:
UnsafeHelper { static Unsafe getUnsafe() { try { Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); return (Unsafe) f.get(null); } catch (NoSuchFieldException | IllegalAccessException e) { throw new RuntimeException("Failed to get unsafe instance, are you running Oracle JDK?", e); } }   static final Unsafe unsafe; }### Answer:
@Test public void testIllegalAccessException() throws Exception { assertNotNull(UnsafeHelper.unsafe); Field field = UnsafeHelperTest.class.getDeclaredField("dummy"); new Expectations(Field.class) {{ field.get(null); result = new IllegalAccessException("test"); }}; try { UnsafeHelper.getUnsafe(); fail("No exception thrown"); } catch (Throwable t) { if (t instanceof RuntimeException) { assertTrue(t.getCause() instanceof IllegalAccessException); assertEquals("test", t.getCause().getMessage()); } else { assertTrue(t.getCause().getCause() instanceof IllegalAccessException); assertEquals("test", t.getCause().getCause().getMessage()); } } }