@Test
public void shouldThrowSystemCommandFailedExceptionIfCommandFails() throws Exception {

    final UUID commandId = UUID.randomUUID();
    final TestCommand testCommand = new TestCommand();
    final SystemCommandException systemCommandException = new SystemCommandException("Ooops");
    final String stackTrace = "stack trace";
    final SystemCommandHandlerProxy systemCommandHandlerProxy = mock(SystemCommandHandlerProxy.class);
    when(systemCommandStore.findCommandProxy(testCommand)).thenReturn(systemCommandHandlerProxy);
    doThrow(systemCommandException).when(systemCommandHandlerProxy).invokeCommand(testCommand, commandId);
    when(stackTraceProvider.getStackTrace(systemCommandException)).thenReturn(stackTrace);
    try {
        systemCommandRunner.run(testCommand, commandId);
        fail();
    } catch (final SystemCommandInvocationFailedException expected) {
        assertThat(expected.getMessage(), is("Failed to run System Command 'TEST_COMMAND'. Caused by uk.gov.justice.services.jmx.api.SystemCommandException: Ooops"));
        assertThat(expected.getServerStackTrace(), is(stackTrace));
        assertThat(expected.getCause(), is(nullValue()));
    }
}