I am fairly new to Playframework (and Scala in general). At the moment I am writing specs2 unit test that test python backend service, using MockWS. Yes, I found it great to test whether my frontend works using the same output as what the backend generated. But what if I really want to test the backend? How can I avoid the use of MockWS?
"SomeService" should {
object Props {
object Urls {
val BaseUrl = "http://localhost:9002/someservice/"
val Test = s"${BaseUrl}test"
}
val test = Test(value=true)
}
class NormalContext extends Scope {
val ws = MockWS {
case (GET, Props.Urls.Test) => Action {
Ok(Json.toJson(Props.test))
}
}
val abtestService = new SomeService(ws, Props.Urls.BaseUrl, 100)
}
"test mock" should {
"return True" when {
"call without any argument" in new NormalContext {
val result = someService.getTest()
whenReady(result) { result =>
result must equal (true)
}
}
}
}
}
I try to introduce this new context, but got the exception: java.lang.RuntimeException: There is no started application
Any suggestion?
class LiveBackEndContext extends Scope {
import play.api.Play.current
val ws: WSClient = WS.client
val abtestService = new AbTestService(ws, Props.Urls.BaseUrl, 100)
}
"test without mock" should {
"return True" when {
"call without any argument" in new LiveBackEndContext {
val result = abtestService.getTest()
whenReady(result) { result =>
result must equal (true)
}
}
}
}
Related
I have a service class in my project and I want to test one of its methods that is performing an api call, so I want to catch this call and return something fake so I can test my method, it looks like this:
class MyService #Inject()(implicit config: Configuration, wsClient: WSClient) {
def methodToTest(list: List[String]): Future[Either[BadRequestResponse, Unit]] = {
wsClient.url(url).withHeaders(("Content-Type", "application/json")).post(write(list)).map { response =>
response.status match {
case Status.OK =>
Right(logger.debug("Everything is OK!"))
case Status.BAD_REQUEST =>
Left(parse(response.body).extract[BadRequestResponse])
case _ =>
val ex = new RuntimeException(s"Failed with status: ${response.status} body: ${response.body}")
logger.error(s"Service failed: ", ex)
throw ex
}
}
}
}
and now in my test class I go:
class MyServiceTest extends FreeSpec with ShouldMatchers with OneAppPerSuite with ScalaFutures with WsScalaTestClient {
implicit lazy val materializer: Materializer = app.materializer
lazy val config: Configuration = app.injector.instanceOf[Configuration]
lazy val myService = app.injector.instanceOf[MyService]
"My Service Tests" - {
"Should behave as im expecting" in {
Server.withRouter() {
case POST(p"/fake/api/in/conf") => Action { request =>
Results.Ok
}
} { implicit port =>
WsTestClient.withClient { implicit client =>
whenReady(myService.methodToTest(List("1","2","3"))) { res =>
res.isRight shouldBe true
}
}
}
}
}
}
and I get this error:
scheme java.lang.NullPointerException: scheme
also tried put under client => :
val myService = new MyService {
implicit val config: Configuration = configuration
implicit val ws: WSClient = client
}
but got some other error that I dont have enough arguments in the constructor...
why is it not working?
if there is a better nd simpler way to fake this api call i will love to hear it :)
thanks!
Server.withRouter may not be exactly what you want. It creates a server and bound it to a random port, per instance (unless you specify the port). It also creates its own instance of application which will be disconnected from the app you used to instantiate the service.
Another thing is that the injected WSClient do not works relative to your application. You need to use the client which is passed to WsTestClient.withClient block instead. So, you should do something like:
class MyServiceTest extends FreeSpec with ShouldMatchers with OneAppPerSuite with ScalaFutures with WsScalaTestClient {
implicit lazy val materializer: Materializer = app.materializer
lazy val config: Configuration = app.injector.instanceOf[Configuration]
"My Service Tests" - {
"Should behave as im expecting" in {
Server.withRouter() {
case POST(p"/fake/api/in/conf") => Action { request =>
Results.Ok
}
} { implicit port =>
WsTestClient.withClient { implicit client =>
// Use the client "instrumented" by Play. It will
// handle the relative aspect of the url.
val myService = new MyService(client, config)
whenReady(myService.methodToTest(List("1","2","3"))) { res =>
res.isRight shouldBe true
}
}
}
}
}
}
I am using specs2 as my test framework.
I want to generate a uniq key that will be available in the test itself.
def around[R: AsResult](r: => R): Result = {
val uniqueToken = before()
try AsResult(r)(uniqueToken)
finally after(uniqueToken)
}
"foo" should {
"bar" in {
do something with uniqueToken
}
}
Couldn't find any good way to do it..
Any idea?
You can write this
class MySpec extends Specification with ForEach[Token] {
"foo" should {
"do something" in { token: Token =>
ok
}
}
def foreach[R : AsResult](f: Token => R): Result = {
val token = createToken
try AsResult(f(token))
finally cleanup(token)
}
}
This is documented here.
You should get the general idea from this pseudocode:
class Around[R: AsResult](r: => R) {
val uniqueToken = before()
try AsResult(r)(uniqueToken)
finally after(uniqueToken)
}
"foo" should {
"bar" in new Around(r) {
do something with uniqueToken
}
}
I have a class with a single method that I want to unit-test:
#Singleton
class RegistrationWorkflow #Inject()(userService: UserService,
addUserValidator: RegisterUserValidator,
db: Database) {
def registerUser(registerForm: RegisterUserForm): Future[Vector[FormError]] = {
val dbActions = addUserValidator.validate(registerForm).flatMap({ validation =>
if (validation.isEmpty) {
userService.add(User(GUID.shortGuid(),
registerForm.username,
registerForm.email,
BCrypt.hashpw(registerForm.password, BCrypt.gensalt())))
.andThen(DBIO.successful(validation))
} else {
DBIO.successful(validation)
}
}).transactionally
db.run(dbActions)
}
}
addUserValidator validates the form and returns a Vector of form errors. If there were no errors, the user is inserted into database. I am returning the form errors because in the controller I'm either returning a 201 or a 400 with a list of errors.
I have written a specs2 test for this:
class RegistrationWorkflowTest extends Specification with Mockito with TestUtils {
"RegistrationControllerWorkflow.registerUser" should {
"insert a user to database if validation succeeds" in new Fixture {
registerUserValidatorMock.validate(testUserFormData) returns DBIO.successful(Vector())
userServiceMock.add(any) returns DBIO.successful(1)
val result = await(target.registerUser(testUserFormData))
result.isEmpty must beTrue
there was one(registerUserValidatorMock).validate(testUserFormData)
there was one(userServiceMock).add(beLike[User] { case User(_, testUser.username, testUser.email, _) => ok })
}
"return error collection if validation failed" in new Fixture {
registerUserValidatorMock.validate(testUserFormData) returns DBIO.successful(Vector(FormError("field", Vector("error"))))
val result = await(target.registerUser(testUserFormData))
result.size must beEqualTo(1)
result.contains(FormError("field", Vector("error"))) must beTrue
there was one(registerUserValidatorMock).validate(testUserFormData)
there was no(userServiceMock).add(any)
}
}
trait Fixture extends Scope with MockDatabase {
val userServiceMock = mock[UserService]
val registerUserValidatorMock = mock[RegisterUserValidator]
val target = new RegistrationWorkflow(userServiceMock, registerUserValidatorMock, db)
val testUser = UserFactory.baseUser()
val testUserFormData = RegisterUserFactory.baseRegisterUserForm()
}
}
The issue with this test is that it just asserts that userService.add was called. This means that I can change my implementation to the following:
val dbActions = addUserValidator.validate(registerForm).flatMap({ validation =>
if (validation.isEmpty) {
userService.add(User(GUID.shortGuid(),
registerForm.username,
registerForm.email,
BCrypt.hashpw(registerForm.password, BCrypt.gensalt())))
DBIO.successful(validation)
} else {
DBIO.successful(validation)
}
}).transactionally
db.run(dbActions)
The test still passes, but the user will not be inserted, because I am not ussing andThen combinator on the DBIO that was returned by userService.add method.
I know that I could use an in memory database and then assert that the user was actually inserted, but I don't want to do that because I already tested userService.add method separately with an in memory database and now I want to test registerUser method without calling any dependencies.
I'm testing how a new Actor I'm working on handles unexpected messages. I'd like to assert that it throws a GibberishException in these cases. Here's the test and the implementation so far:
Test:
"""throw a GibberishException for unrecognized messages""" in {
//define a service that creates gibberish-speaking repositories
val stubs = new svcStub(
actorOf(new Actor{
def receive = { case _ => {
self.channel ! "you're savage with the cabbage"
}
}
})
)
val model = actorOf(new HomeModel(stubs.svc,stubs.store))
val supervisor = Supervisor(
SupervisorConfig(
OneForOneStrategy(List(classOf[Exception]), 3, 1000),
Supervise(model,Permanent) :: Nil
)
)
try{
intercept[GibberishException] {
supervisor.start
model !! "plan"
}
} finally {
supervisor.shutdown
}
stubs.store.plan should equal (null)
stubs.svcIsOpen should be (false)
}
Implementation:
class HomeModel(service: PlanService, store: HomeStore)
extends Actor {
private val loaderRepo = service.getRepo()
private var view: Channel[Any] = null
override def postStop() = {
service.close()
}
def receive = {
case "plan" => {
view=self.channel
loaderRepo ! LoadRequest()
}
case p: Plan => {
store.plan=p
view ! store.plan
}
case _ => throw new GibberishException(_)
}
}
However, when I run the test, the exception details get to the Supervisor I established, but I don't know how to do anything with them (like log them or test their type). I'd like to be able to get the exception details here from the supervisor so i can rethrow and intercept them in my test. Outside of a test method, I could imagine this being useful if you wanted to report the nature of an exception in the UI of a running app. Is there a way to get this from the Supervisor when it happens?
Change the OneForOneStrategy to only handle GibberishException, should solve it.
I am not a Groovy expert, but I did read the book "Groovy in Action". In Groovy, each closure comes with a "context", where the items inside the closure can get access to pseudo-variables like "this", "owner", and "delegate", that let the items know who called the closure. This allows one to write DSLs like this (from Groovy in Action):
swing = new SwingBuilder()
frame = swing.frame(title:'Demo') {
menuBar {
menu('File') {
menuItem 'New'
menuItem 'Open'
}
}
panel {
// ...
}
}
Note that 'menuBar' "knows" that it belongs to 'frame' because it can get context information about the owner and delegate of the closure.
Is this possible to do in Scala? If so, how?
One way is to use a scala.util.DynamicVariable to track the context. Something like the SwingBuilder could be implemented as
import scala.util.DynamicVariable
import javax.swing._
object SwingBuilder {
case class Context(frame: Option[JFrame], parent: Option[JComponent])
}
class SwingBuilder {
import SwingBuilder._
val context = new DynamicVariable[Context](Context(None,None))
def frame(title: String)(f: =>Unit) = {
val res = new JFrame(title)
res.add(new JPanel())
context.withValue(Context(Some(res),context.value.parent)){f;res}
}
def menuBar(f: =>Unit) = {
val mb = new JMenuBar()
context.value.frame.foreach(_.setJMenuBar(mb))
context.withValue(Context(context.value.frame,Some(mb))){f;mb}
}
def menu(title: String)(f: =>Unit) = {
val m = new JMenu(title)
context.value.parent.foreach(_.asInstanceOf[JMenuBar].add(m))
context.withValue(Context(context.value.frame,Some(m))){f;m}
}
def menuItem(title: String) = {
val mi = new JMenuItem(title)
context.value.parent.foreach(_.asInstanceOf[JMenu].add(mi))
}
}
object Test {
def main(args: Array[String]) {
val builder = new SwingBuilder()
import builder._
val f = frame("Demo") {
val mb = menuBar {
menu("File") {
menuItem("New")
menuItem("Open")
}
}
}
f.setVisible(true)
}
}