본문 바로가기

Development/Web & Server

[Play Framework] Scala Test


1. Test 폴더에 Scala 테스트 클래스를 작성한다.


2.  관련 라이브러리 import

import play.api.test._
import play.api.test.Helpers._


3. Application 상황에서 테스트를 하고 싶다면

val fakeApplicationWithGlobal = FakeApplication(withGlobal = Some(new GlobalSettings() {
  override def onStart(app: Application) { println("Hello world!") }
}))


4. 테스트 Object 작성

object ExamplePlaySpecificationSpec extends PlaySpecification {
  "The specification" should {

    "have access to HeaderNames" in {
      USER_AGENT must be_===("User-Agent")
    }

    "have access to Status" in {
      OK must be_===(200)
    }
  }
}

5. fakeApplication을 로딩후에 테스트를 하는 코드 예제

"Company User" should {
"Join Test" in new WithApplication(fakeApplicationWithGlobal) {
val newUser = User(0,"Testid", "1111111")
val joinUser = UserService.join(newUser)
joinUser must be_!= (null)
}
}


참고 : https://www.playframework.com/documentation/2.4.x/ScalaFunctionalTestingWithSpecs2