The requested route has not been mapped in Spark?

36
January 09, 2019, at 03:30 AM

I have simple restfull api in Java Spark. Unit test which is getting single user passes sucesfully as expected instead of failure case with non existing user. Unit test:

@Test
public void testGetNonExistingUser() {
    String userId = "5284c69d-ab72-4703-ba84-ce4feafd456f";
    Response response = request("GET", "/user/" + userId);
}

Returns output:

100 [Thread-0] INFO org.eclipse.jetty.util.log - Logging initialized @620ms
141 [Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - == Spark has ignited ...
141 [Thread-0] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >> Listening on 0.0.0.0:4567
143 [Thread-0] INFO org.eclipse.jetty.server.Server - jetty-9.3.6.v20151106
204 [Thread-0] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@575743e2{HTTP/1.1,[http/1.1]}{0.0.0.0:4567}
204 [Thread-0] INFO org.eclipse.jetty.server.Server - Started @725ms
2019-01-07 20:54:35 ERROR String:79 - checkIfIdAlreadyExists() User with id=5284c69d-ab72-4703-ba84-ce4feafd456f not exists
285 [qtp1828303595-19] INFO spark.http.matching.MatcherFilter - The requested route [/user/5284c69d-ab72-4703-ba84-ce4feafd456f] has not been mapped in Spark
java.io.FileNotFoundException: http://localhost:4567/user/5284c69d-ab72-4703-ba84-ce4feafd456f
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1909)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
(...)
300 [main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - >>> Spark shutting down ...
308 [main] INFO org.eclipse.jetty.server.ServerConnector - Stopped ServerConnector@575743e2{HTTP/1.1,[http/1.1]}{0.0.0.0:4567}
310 [main] INFO spark.embeddedserver.jetty.EmbeddedJettyServer - done
java.lang.AssertionError: Sending request failed: http://localhost:4567/user/5284c69d-ab72-4703-ba84-ce4feafd456f
    at org.junit.Assert.fail(Assert.java:88)
    at com.endpoints.moneyapp.utils.Utils.request(Utils.java:40)
(...)

Code which is handling this exception in spark.

get("/user/:id", (request, response) -> userDAO.getUser(request.params(":id")), JSONUtil.json());
after((request, response) -> {
    response.type("application/json");
});
exception(IllegalArgumentException.class, (exception, request, response) -> {
    response.status(FAILED_RESPONSE);
    response.body(JSONUtil.toJson(new ResponseError(exception)));
});
exception(CustomException.class, (exception, request, response) -> {
    response.status(FAILED_RESPONSE);
    response.body(JSONUtil.toJson(new ResponseError(exception)));
});

In the code there is a place where exception is throwing.

private void checkIfIdAlreadyExists(String id) throws CustomException {
if (!users.containsKey(id)) {
    String message = "User with id=" + id + " not exists";
    throw new CustomException(message);
}
}

How to proper pass unit test with non existing user?

READ ALSO
Have a Queue subscribed to multiple Queues, programmaticaly

Have a Queue subscribed to multiple Queues, programmaticaly

I have two Virtual Topics: T1, T2I have a consumer named FOO

48
Java get keyboard key typed in a function

Java get keyboard key typed in a function

So I am making a game in java, and I am currently working with keyboard inputI have a keylistener class setup called KeyboardManager and in it a static function that detects if a key is being held down

13
PHP Date Format returning incorrect suffix ('S')

PHP Date Format returning incorrect suffix ('S')

I have a Unix Timestamps such as 1423389830 and 1511367992I'm using a simple date() function: date( 'M nS, Y', $timestamp ); and it seems like it's returning the wrong date, with the appropriate suffix - which output some rather silly results

40
Need Dynamic PHP Variable help setting variable name that points to another query result

Need Dynamic PHP Variable help setting variable name that points to another query result

I have a php page that loops over a list of parts from the database

32