Play Framework TDD exception testing routes

Asked

Viewed 67 times

0

I am studying the Play Framework with Java and in the documentation there is an example of route testing at the end of this page: Doc Play Framework

But when trying to run the test he answers me with one exception because the route in question does not exist. I know this because when testing with a registered route it from the success message.

That river "/users/new" does not exist. My test code:

26 @Test
27 public void TestandoLigacaoeRota(){
28      RequestBuilder request = new RequestBuilder()
29              .method(GET)
30              .uri("/usuarios/novo");
31
32      try {
33          Result result = route(request);
34          assertEquals(NOT_FOUND, result.status());
35      } catch (Exception e){
36          e.printStackTrace();
37      }
38 }

Log of the exception:

java.lang.RuntimeException: There is no started application
    at scala.sys.package$.error(package.scala:27)
    at play.api.Play$$anonfun$current$1.apply(Play.scala:71)
    at play.api.Play$$anonfun$current$1.apply(Play.scala:71)
    at scala.Option.getOrElse(Option.scala:121)
    at play.api.Play$.current(Play.scala:71)
    at play.api.Play.current(Play.scala)
    at play.Play.application(Play.java:17)
    at play.test.Helpers.route(Helpers.java:412)
    at play.test.Helpers.route(Helpers.java:408)
    at controllers.UsuariosTest.TestandoLigacaoeRota(UsuariosTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

1 answer

0


After researching a little better I found the solution to the problem.

    @Test
    public void TestandoLigacaoeRota(){
        running(fakeApplication(inMemoryDatabase("test")), () -> {
            RequestBuilder request = new RequestBuilder()
                    .method(GET)
                    .uri("/usuarios/not_found");

            Result result = route(request);
            assertEquals(NOT_FOUND, result.status());
        });
    }

But like I’m new to both TDD and Play I think q in the Doc example should be correct, but ran() lacked the code to work. Tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.