InternalServerError extends Component implements HasErrorParameter<Exception> { @Override public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<Exception> parameter) { String exceptionText; if (parameter.hasCustomMessage()) { exceptionText = String.format( "There was an exception while trying to navigate to '%s'" + " with the exception message '%s'", event.getLocation().getPath(), parameter.getCustomMessage()); } else { exceptionText = String.format( "There was an exception while trying to navigate to '%s'", event.getLocation().getPath()); } Exception exception = parameter.getException(); if (exception != null) { reportException(exception, event.getLocation().getPath(), exceptionText); } else { getElement().setText(exceptionText); } return HttpServletResponse.SC_INTERNAL_SERVER_ERROR; }  @Override int setErrorParameter(BeforeEnterEvent event,
            ErrorParameter<Exception> parameter);  }
@Test public void productionMode_noWarningAndStacktrace() { Mockito.when(configuration.isProductionMode()).thenReturn(true); InternalServerError testInstance = new InternalServerError(); testInstance.setErrorParameter(event, new ErrorParameter<>( Exception.class, new NullPointerException("foo"))); Assert.assertEquals( "Only a text node with exception message should be shown", 1, testInstance.getElement().getChildCount()); }
@Test public void nonProductionMode_noLogBinding_showWaringAndStacktrace() { Mockito.when(configuration.isProductionMode()).thenReturn(false); InternalServerError testInstance = new InternalServerError() { @Override protected boolean hasLogBinding() { return false; } }; testInstance.setErrorParameter(event, new ErrorParameter<>( Exception.class, new NullPointerException("foo"))); Assert.assertEquals( "3 elements should be shown: exception text, warning about log binding absence and exception stacktrace", 3, testInstance.getElement().getChildCount()); Element warning = testInstance.getElement().getChild(1); Assert.assertEquals("div", warning.getTag()); Assert.assertTrue(warning.getText().contains("SLF4J")); Element stacktrace = testInstance.getElement().getChild(2); Assert.assertEquals("pre", stacktrace.getTag()); Assert.assertTrue(stacktrace.getText() .contains(NullPointerException.class.getName())); Assert.assertTrue(stacktrace.getText().contains("foo")); }
@Test public void nonProductionMode_hasLogBinding_showStacktraceAndNoWarning() { Mockito.when(configuration.isProductionMode()).thenReturn(false); InternalServerError testInstance = new InternalServerError() { @Override protected boolean hasLogBinding() { return true; } }; testInstance.setErrorParameter(event, new ErrorParameter<>( Exception.class, new NullPointerException("foo"))); Assert.assertEquals( "2 elements should be shown: exception text and exception stacktrace", 2, testInstance.getElement().getChildCount()); Element stacktrace = testInstance.getElement().getChild(1); Assert.assertEquals("pre", stacktrace.getTag()); Assert.assertTrue(stacktrace.getText() .contains(NullPointerException.class.getName())); Assert.assertTrue(stacktrace.getText().contains("foo")); }