FaultServices { public Fault buildFault(Throwable ex) { Fault fault; int stacktracelength = configuration.getStacktracelength(); if (stacktracelength == 0 || logger.isDebugEnabled()) { logger.error("Invocation failed", ex); } fault = new Fault(ex, stacktracelength); return fault; }  Fault buildFault(Throwable ex);  }
@Test public void testBuildFault() { System.out.println("buildFault"); when(configuration.getStacktracelength()).thenReturn(1).thenReturn(3); try { throw new Exception("ERROR_MESSAGE"); } catch (Exception e) { Fault fault = instance.buildFault(e); assertThat(fault.getClassname()).isEqualTo("java.lang.Exception"); assertThat(fault.getMessage()).isEqualTo("ERROR_MESSAGE"); String[] stacktraces = fault.getStacktrace(); assertThat(stacktraces).hasSize(1); assertThat(stacktraces[0]).startsWith(this.getClass().getName()+".testBuildFault("+this.getClass().getSimpleName()+".java:"); fault = instance.buildFault(e); stacktraces = fault.getStacktrace(); assertThat(stacktraces).hasSize(3); } }
@Test public void testBuildFaultDebugMode() { System.out.println("buildFault"); when(configuration.getStacktracelength()).thenReturn(3); when(logger.isDebugEnabled()).thenReturn(true); try { throw new Exception("ERROR_MESSAGE"); } catch (Exception e) { Fault fault = instance.buildFault(e); ArgumentCaptor<String> captureLog = ArgumentCaptor.forClass(String.class); verify(logger).error(captureLog.capture(), any(Throwable.class)); assertThat(captureLog.getValue()).isEqualTo("Invocation failed"); assertThat(fault.getClassname()).isEqualTo("java.lang.Exception"); assertThat(fault.getMessage()).isEqualTo("ERROR_MESSAGE"); String[] stacktraces = fault.getStacktrace(); assertThat(stacktraces).hasSize(3); } }
@Test public void testBuildFaultNoStackTrace() { System.out.println("buildFault"); when(configuration.getStacktracelength()).thenReturn(0); try { throw new Exception("ERROR_MESSAGE"); } catch (Exception e) { Fault fault = instance.buildFault(e); assertThat(fault.getClassname()).isEqualTo("java.lang.Exception"); assertThat(fault.getMessage()).isEqualTo("ERROR_MESSAGE"); String[] stacktraces = fault.getStacktrace(); assertThat(stacktraces).isEmpty(); } }