### Question:
SecuredMethodInvocationHelper { static ExecutionContext getContextAsFirstArgumentOfSecureObject(Object secureObject) { Object[] args; if (secureObject instanceof MethodInvocation) { MethodInvocation invocation = (MethodInvocation) secureObject; args = invocation.getArguments(); } else { throw new AuthorizationServiceException("Secure object: " + secureObject + " is not a MethodInvocation"); } ExecutionContext context = null; if (isNotEmpty(args) && args[0] instanceof ExecutionContext) { context = (ExecutionContext) args[0]; } return context; }    }### Answer:
@Test(expected = AuthorizationServiceException.class) public void getContextAsFirstArgumentOfSecureObject_securedObjIsNotMethodInvocation_throwException() { Object securedObject = mock(Object.class); getContextAsFirstArgumentOfSecureObject(securedObject); }
@Test public void getContextAsFirstArgumentOfSecureObject_methodInvocationNotContainsArguments_returnNull() { MethodInvocation securedObject = mock(MethodInvocation.class); when(securedObject.getArguments()).thenReturn(null); assertNull(getContextAsFirstArgumentOfSecureObject(securedObject)); }
@Test public void getContextAsFirstArgumentOfSecureObject_methodInvocationNotContainsNotExecutionContext_returnNull() { MethodInvocation securedObject = mock(MethodInvocation.class); when(securedObject.getArguments()).thenReturn(new Object[]{"one", "two"}); assertNull(getContextAsFirstArgumentOfSecureObject(securedObject)); }
@Test public void getContextAsFirstArgumentOfSecureObject_methodInvocationContainsExecutionContextAsFirstArg_returnExecutionContext() { MethodInvocation securedObject = mock(MethodInvocation.class); ExecutionContext mockExecutionContext = mock(ExecutionContext.class); when(securedObject.getArguments()).thenReturn(new Object[]{mockExecutionContext, "two"}); assertEquals(mockExecutionContext, getContextAsFirstArgumentOfSecureObject(securedObject)); }