Peter Schwarz
Peter Schwarz

Reputation: 352

Playframework tests pass in Intellij but not in play console

I have a Simple insert and update test for a table:

    case class Player(id: UUID, name: String, email: String)

with all the appropriate setup in a model object for performing inserts and finds.

Running the following test:

class PlayerSpec extends Specification {
  "Player" should {

    val application = FakeApplication(additionalConfiguration =  inMemoryDatabase("test"))

    "insert a player" in {
      running(application) {

        val uuid = UUID.randomUUID()

        Player.insert(Player(uuid, "Joe Person", "[email protected]")) must beEqualTo(1)
      }
    }

    "Find a player by id" in {
      running(application) {
        val uuid = UUID.randomUUID()

        Player.insert(Player(uuid, "Jane Person", "[email protected]"))

        Player.findById(uuid) must beEqualTo(Some(Player(uuid, "Jane Person", "[email protected]")))
      }
    }

  }
}

I get a green bar in Intellij (both examples pass). However, when I run this in the play console I get the following error:

[info] PlayerSpec
[info] 
[info] Player should
[info] + insert a player
[error] ! Find a player by id
[error]     anon$1: Configuration error [Cannot connect to database [default]] Configuration.scala:258)
 ....

Is this a configuration issue, or am I missing some sort of a beforeEach type call?

EDIT

Just adding the db code, for more reference

case class Player(id: UUID, name: String, email: String)

object Player {

  private val fullPlayer = {
    get[UUID]("player.id")(Utils.rowToUuid) ~
    get[String]("player.name") ~
    get[String]("player.email") map {
      case id~name~email => Player(id, name, email)
    }
  }

  def insert(player: Player) = {
    DB.withConnection { implicit c =>

      SQL(
        """
          insert into player (id, name, email) values (
             {id}, {name}, {email}
          )
        """
      ).on(
        'id -> player.id.toString,
        'name -> player.name,
        'email -> player.email
      ).executeUpdate()

    }
  }

  def findById(id: UUID) : Option[Player] = {
    DB.withConnection { implicit c =>
      SQL("select * from player where id = {id}").on('id -> id).as(fullPlayer.singleOpt)
    }
  }
}

rowToUUID:

object Utils {
  implicit def rowToUuid: Column[UUID] = Column.nonNull {
    (value, meta) =>
      val MetaDataItem(qualified, nullable, clazz) = meta
      value match {
        case uuid: UUID => Right(uuid)
        case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to UUID for column " + qualified))
      }
  }
}

Upvotes: 1

Views: 803

Answers (1)

Schleichardt
Schleichardt

Reputation: 7552

You should not reuse a FakeApplication for multiple tests.

Just change

 val application = FakeApplication(additionalConfiguration =  inMemoryDatabase("test"))

to

def application() = FakeApplication(additionalConfiguration =  inMemoryDatabase("test"))

To produce a new FakeApplication each time you call application. It would be good style to call this function with parenthesis.

Upvotes: 1

Related Questions