Please see the code snippet below illustrating an issue I encountered when testing an ErrorHandler with MockRouter's tryError method.
The snippet does the following:
- Configures a Jooby application with a custom
ErrorHandler that catches CustomException.
- Asserts the custom
ErrorHandler by creating a MockRouter and invoking its tryError method.
Expectation
- The assertion in
ApplicationTest::test runs and passes.
Observed behaviour
Example code snippet
package joobysandbox;
import io.jooby.Jooby;
import io.jooby.StatusCode;
import io.jooby.problem.HttpProblem;
import io.jooby.test.MockContext;
import io.jooby.test.MockRouter;
import org.junit.jupiter.api.Test;
import static joobysandbox.Application.CustomException;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ApplicationTest {
private final MockRouter router = new MockRouter(new Application()).setFullExecution(true);
@Test
void test() {
var ctx = new MockContext();
router.tryError(new CustomException(), ctx); // throws java.lang.NullPointerException
assertEquals(StatusCode.BAD_REQUEST, ctx.getResponseCode());
}
}
class Application extends Jooby {
{
get("/throw", _ -> {
throw new CustomException();
});
error(CustomException.class, (ctx, _, statusCode) -> {
var problem = HttpProblem.badRequest("A Client Error — Obviously");
ctx.getRouter().getErrorHandler().apply(ctx, problem, statusCode);
});
}
public static void main(String[] args) {
runApp(args, Application::new);
}
static class CustomException extends RuntimeException {}
}
Expectation details
java.lang.NullPointerException: Cannot invoke "io.jooby.Router.getErrorHandler()" because the return value of "io.jooby.Context.getRouter()" is null
at joobysandbox.Application.lambda$new$0(Application.java:34)
at io.jooby.Router.lambda$error$0(Router.java:747)
at io.jooby.test.MockRouter.tryError(MockRouter.java:476)
at joobysandbox.ApplicationTest.test(Application.java:20)
Environment
jooby and jooby-test v4.0.7
junit-jupiter v6.0.0
JDK 24
Please see the code snippet below illustrating an issue I encountered when testing an
ErrorHandlerwithMockRouter'stryErrormethod.The snippet does the following:
ErrorHandlerthat catchesCustomException.HttpProblemand delegates to the underlyingErrorHandler, following the Error Handler - Problem Details - Custom Problems section of the documentation.ErrorHandlerby creating aMockRouterand invoking itstryErrormethod.Expectation
ApplicationTest::testruns and passes.Observed behaviour
MockRouter::tryErrorcall inApplicationTest::testthrows aNullPointerException. See the exception details below.Example code snippet
Expectation details
Environment
jooby and jooby-test v4.0.7
junit-jupiter v6.0.0
JDK 24