Skip to content

Commit 6c1629d

Browse files
committed
Large test case broken down into components
1 parent 920320a commit 6c1629d

1 file changed

Lines changed: 29 additions & 22 deletions

File tree

‎ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import ratpack.exec.Promise;
66
import ratpack.func.Action;
77
import ratpack.handling.Chain;
8+
import ratpack.handling.Handler;
89
import ratpack.registry.Registry;
910
import ratpack.test.embed.EmbeddedApp;
1011
import ratpack.test.exec.ExecHarness;
@@ -15,37 +16,43 @@ public class AppHttpUnitTest {
1516

1617
@Test
1718
public void givenAnyUri_GetEmployeeFromSameRegistry() throws Exception {
18-
final Action<Chain> chainAction = chain -> chain.prefix("employee/:id", empChain -> empChain.all(ctx -> {
19+
Handler allHandler = ctx -> {
1920
Long id = Long.valueOf(ctx.getPathTokens()
20-
.get("id"));
21+
.get("id"));
2122
Employee employee = new Employee(id, "Mr", "NY");
2223
ctx.next(Registry.single(Employee.class, employee));
23-
})
24-
.get("name", ctx -> {
25-
Employee employee = ctx.get(Employee.class);
26-
ctx.getResponse()
27-
.send("Name of employee with ID " + employee.getId() + " is " + employee.getName());
28-
})
29-
.get("title", ctx -> {
30-
Employee employee = ctx.get(Employee.class);
31-
ctx.getResponse()
32-
.send("Title of employee with ID " + employee.getId() + " is " + employee.getTitle());
33-
}));
24+
};
25+
Handler empNameHandler = ctx -> {
26+
Employee employee = ctx.get(Employee.class);
27+
ctx.getResponse()
28+
.send("Name of employee with ID " + employee.getId() + " is " + employee.getName());
29+
};
30+
Handler empTitleHandler = ctx -> {
31+
Employee employee = ctx.get(Employee.class);
32+
ctx.getResponse()
33+
.send("Title of employee with ID " + employee.getId() + " is " + employee.getTitle());
34+
};
35+
36+
Action<Chain> chainAction = chain -> chain.prefix("employee/:id", empChain -> {
37+
empChain.all(allHandler)
38+
.get("name", empNameHandler)
39+
.get("title", empTitleHandler);
40+
});
3441
EmbeddedApp.fromHandlers(chainAction)
35-
.test(testHttpClient -> {
36-
assertEquals("Name of employee with ID 1 is NY", testHttpClient.get("employee/1/name")
37-
.getBody()
38-
.getText());
39-
assertEquals("Title of employee with ID 1 is Mr", testHttpClient.get("employee/1/title")
40-
.getBody()
41-
.getText());
42-
});
42+
.test(testHttpClient -> {
43+
assertEquals("Name of employee with ID 1 is NY", testHttpClient.get("employee/1/name")
44+
.getBody()
45+
.getText());
46+
assertEquals("Title of employee with ID 1 is Mr", testHttpClient.get("employee/1/title")
47+
.getBody()
48+
.getText());
49+
});
4350
}
4451

4552
@Test
4653
public void givenSyncDataSource_GetDataFromPromise() throws Exception {
4754
String value = ExecHarness.yieldSingle(execution -> Promise.sync(() -> "Foo"))
48-
.getValueOrThrow();
55+
.getValueOrThrow();
4956
assertEquals("Foo", value);
5057
}
5158
}

0 commit comments

Comments
 (0)