I am trying to write a test case for my application with akka-http
. One of the testcases is given below:
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.testkit.{ ScalatestRouteTest}
import com.reactore.common.core.{CommonCoreSystem, CommonActors, BootedCommonCoreSystem}
import scala.concurrent.duration._
import com.reactore.common.feature.referencedata.{DepartmentRepository, DepartmentService, DepartmentController, DepartmentRest}
import org.scalatest.concurrent.AsyncAssertions
import org.scalatest.time.Span
import org.scalatest.{WordSpec, Matchers}
import akka.http.scaladsl.model.StatusCodes._
/**
* Created by krishna on 18/6/15.
*/
class DepartmentITTest extends WordSpec with Matchers with ScalatestRouteTest with CommonCoreSystem with CommonActors {
// override val departmentRouter = system.actorOf(Props(classOf[DepartmentService], DepartmentRepository), Constants.DEPARTMENT_ROUTER_NAME)
val deptRoute = (new DepartmentRest(departmentRouter)(DepartmentController).deptRoutes)
implicit val timeout = AsyncAssertions.timeout(Span.convertDurationToSpan(5.second))
val departmentJson = """{"id":13,"name":"ENGP22","shortCode":"ENGP2","parentDepartmentId":null,"mineId":null,"photoName":null,"isRemoved":false,"isPendingForApproval":false,"createdBy":0,"createDate":"2015-03-09 00:00:00","modifiedBy":0,"modifiedDate":"2015-03-09 00:00:00"}"""
val header = RawHeader("apiKey", "xxxxx")
"Service" should {
"return department by id" in {
Get("/departments/13").withHeaders(header) ~> deptRoute ~> check {
// Thread.sleep(500)
status shouldBe OK
responseAs[String] shouldBe departmentJson
}
}
}
}
When I run this, it works correctly sometimes, and sometimes I get the error as Request was neither completed nor rejected within 1 second
. I added a Thread.sleep for making it work now. I know that is not the correct solution. Could anyone tell me how to make the test wait for more than 1 second?
The following is working for me: