+doc add Java testkit documentation and scaladoc
This commit is contained in:
parent
f889112096
commit
4c24b72b31
7 changed files with 237 additions and 4 deletions
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.http.javadsl.testkit;
|
||||
|
||||
//#simple-app
|
||||
import akka.http.javadsl.server.*;
|
||||
import akka.http.javadsl.server.values.Parameters;
|
||||
|
||||
public class MyAppService extends HttpApp {
|
||||
RequestVal<Double> x = Parameters.doubleValue("x");
|
||||
RequestVal<Double> y = Parameters.doubleValue("y");
|
||||
|
||||
public RouteResult add(RequestContext ctx, double x, double y) {
|
||||
return ctx.complete("x + y = " + (x + y));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Route createRoute() {
|
||||
return
|
||||
route(
|
||||
get(
|
||||
pathPrefix("calculator").route(
|
||||
path("add").route(
|
||||
handleReflectively(this, "add", x, y)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
//#simple-app
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
|
||||
package docs.http.javadsl.testkit;
|
||||
|
||||
//#simple-app-testing
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.model.StatusCodes;
|
||||
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||
import akka.http.javadsl.testkit.TestRoute;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestkitExampleTest extends JUnitRouteTest {
|
||||
TestRoute appRoute = testRoute(new MyAppService().createRoute());
|
||||
|
||||
@Test
|
||||
public void testCalculatorAdd() {
|
||||
// test happy path
|
||||
appRoute.run(HttpRequest.GET("/calculator/add?x=4.2&y=2.3"))
|
||||
.assertStatusCode(200)
|
||||
.assertEntity("x + y = 6.5");
|
||||
|
||||
// test responses to potential errors
|
||||
appRoute.run(HttpRequest.GET("/calculator/add?x=3.2"))
|
||||
.assertStatusCode(StatusCodes.NOT_FOUND) // 404
|
||||
.assertEntity("Request is missing required query parameter 'y'");
|
||||
|
||||
// test responses to potential errors
|
||||
appRoute.run(HttpRequest.GET("/calculator/add?x=3.2&y=three"))
|
||||
.assertStatusCode(StatusCodes.BAD_REQUEST)
|
||||
.assertEntity("The query parameter 'y' was malformed:\n" +
|
||||
"'three' is not a valid 64-bit floating point value");
|
||||
}
|
||||
}
|
||||
//#simple-app-testing
|
||||
Loading…
Add table
Add a link
Reference in a new issue