Play Framework TDD exception testing routes

0

I'm studying the Play Framework with Java and the documentation has a sample route test at the end of this page: Doc Play Framework

But when trying to run the test it responds with one exception because the route in question does not exist. I know this because when I test with a registered route it the message of success.

This uri / 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 }

Exception Log:

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)
    
asked by anonymous 11.12.2015 / 18:07

1 answer

0

After searching 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 as I am newbie in both TDD and Play I think q in the example of Doc should be correct, but lacked running () in the code to work. This is the tip.

    
11.12.2015 / 18:21