Desktop version

Home arrow Computer Science arrow Building Applications with Scala

Source

NGServiceEndpomtControNerTest.scala

Your NGServiceEndpointControllerTest.scala file should contain the following code:

class NGServiceEndpointControllerTest extends PlaySpec with OneServerPerSuite with

OneBrowserPerSuite with HtmlUnitFactory {

val injector = new GuiceApplicationBuilder()

.injector

val ws:WSClient = injector.instanceOf(classOf[WSClient]) import play.api.libs.concurrent.Execution.

Implicits.defaultContext "NGServiceEndpointController" must {

"return a single double" in {

val future = ws.url(s"http://localhost:${port}/double") .get().map { res => res.body }

val response = Await.result(future, 15.seconds) response must not be empty

assert( new java.lang.Double(response) >= 1 )

}

"return a list of 3 doubles" in {

val future = ws.url(s"http://localhost:${port}/doubles/3") .get().map { res => res.body }

val response = Await.result(future, 15.seconds) response must (not be empty and include ("[") and include ("]"))

}

}

}

Here we have to inject the WSClient library so we can call the controller. This controller has two methods like the service we tested before. The second method returns a JSON structure. Then we check for "["and "]" to make sure that the array is present, since this is a list of three numbers.

We use the assert function to check the response from the controller, and to be 100% sure that everything is okay. Let's run it.

$ activator "test-only NGServiceEndpointControllerTest"

Refer to the following screenshot to see the test result:

diego@4wtnds:~/github2/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice$ activator "test-only NGServlceEndpolntControllerTest"

[info] Loading global plugins from /home/diego/.sbt/0.13/plugins

[info] Loading project definition from /home/diego/home2/diego/github/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice/project

[Info] Set current project to ng-microservice (in build file:/home/diego/home2/diego/github/dtegopaeheco/Book_Building_Reacttve_Functional_Scala_Applieations/Chap5/ng-mieroserviee/)

[info] NGServiceEndpointControllerTest:

[info] NGServiceEndpointController [info] - must return a sngle double [info] - must return a list of 3 doubles [info] ScalaTest

[info] Run completed in 5 seconds, 636 milliseconds.

[info] Total number of tests run: 2 [info] Suites: completed 1, aborted Э

[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed.

[info] Passed: Total 2, Failed 0, Errors 0, Passed 2 [success] Total time: 7 s, completed 17/07/2016 01:46:49

diego@4winds:~/github2/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice$ |

Great! We have covered pretty much everything . In this chapter, we ran all kinds of tests. We always used the command $ activator "test-only xxx"; the reason for this is to save time. However, it is very common to run all tests. You can do that in both projects; we have to just type $ activator test. When running all the tests in the ng-microservice project, we get the result shown in the following screenshot:

diego@4winds:-/github2/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice$ activator test [info] Loading global plugins from /home/diego/.sbt/0.13/plugins

[info] Loading project definition from /home/diego/home2/diego/github/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice/project

[info] Set current project to ng-microservice (in build file:/home/diego/home2/diego/github/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice/)

[info] Compiling 2 Scala sources to /home/diego/home2/diego/github/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice/target/scala-2.11/test-classes... [info] NGServicelmplTestSpec:

[info] The NGServicelmpl

[info] - must Generate a Ramdon number

[info] - must Generate a list of 3 Ramdon numbers

[info] NGServiceEndpointControllerTest:

[info] NGServiceEndpointController [info] - must return a sngle double [info] - must return a list of 3 doubles [info] ScalaTest

[info] Run completed in 17 seconds, 210 milliseconds.

[info] Total number of tests run: 4 [info] Suites: completed 2, aborted 0

[info] Tests: succeeded 4, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed.

[info] Passed: Total 4, Failed 0, Errors 0, Passed 4 [success] Total time: 33 s, completed 17/07/2016 01:51:39

diego@4winds:~/github2/diegopacheco/Book_Building_Reactive_Functional_Scala_Applications/Chap5/ng-microservice$ |

On the other hand, running all the tests in the ReactiveWebStore project gives the result shown in the next screenshot:

Summary

In this chapter, you learned how to do tests. We added several tests for your Scala and Play framework projects. You also learned about unit testing principles, testing Scala applications with JUnit, BDD principles, testing with ScalaTest, testing Play framework applications with ScalaTest, and running tests in Activator / SBT.

In the next chapter, you will learn more about persistence using Slick, which is reactive. We will also change our tests a little bit in order to work with a database.

6

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >

Related topics