This repository was archived by the owner on Apr 23, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathUnitTest.java
More file actions
54 lines (46 loc) · 1.63 KB
/
UnitTest.java
File metadata and controls
54 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import akka.actor.ActorSystem;
import controllers.AsyncController;
import controllers.CountController;
import org.junit.Test;
import play.mvc.Result;
import scala.concurrent.ExecutionContextExecutor;
import java.util.concurrent.CompletionStage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static play.test.Helpers.contentAsString;
/**
* Unit testing does not require Play application start up.
*
* https://www.playframework.com/documentation/latest/JavaTest
*/
public class UnitTest {
@Test
public void simpleCheck() {
int a = 1 + 1;
assertThat(a).isEqualTo(2);
}
// Unit test a controller
@Test
public void testCount() {
final CountController controller = new CountController(() -> 49);
Result result = controller.count();
assertThat(contentAsString(result)).isEqualTo("49");
}
// Unit test a controller with async return
@Test
public void testAsync() {
final ActorSystem actorSystem = ActorSystem.create("test");
try {
final ExecutionContextExecutor ec = actorSystem.dispatcher();
final AsyncController controller = new AsyncController(actorSystem, ec);
final CompletionStage<Result> future = controller.message();
// Block until the result is completed
await().untilAsserted(() ->
assertThat(future.toCompletableFuture())
.isCompletedWithValueMatching(result -> contentAsString(result).equals("Hi!"))
);
} finally {
actorSystem.terminate();
}
}
}