Akkahtp中ScalaTest上的空指针异常

Akkahtp中ScalaTest上的空指针异常,scala,scalatest,akka-http,Scala,Scalatest,Akka Http,错误如下 EmployeeRep=> HomeControlSpec 无法理解上述代码尝试的第二条路由/插入出现内部服务器错误的原因,但未能成功 /insert URL点击数据库并将数据插入表中。但当我尝试单元测试时,它给了我以上的错误 [ERROR] [08/31/2020 20:36:15.529] [ScalaTest-run-running-HomeControllerSpec] [akka.actor.ActorSystemImpl(HomeControllerSpec)] Error

错误如下

EmployeeRep=>

HomeControlSpec

无法理解上述代码尝试的第二条路由/插入出现内部服务器错误的原因,但未能成功 /insert URL点击数据库并将数据插入表中。但当我尝试单元测试时,它给了我以上的错误

[ERROR] [08/31/2020 20:36:15.529] [ScalaTest-run-running-HomeControllerSpec] [akka.actor.ActorSystemImpl(HomeControllerSpec)] Error during processing of request: 'java.lang.NullPointerException (No error message supplied)'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
    java.lang.NullPointerException
        at controller.HomeController$.$anonfun$route$6(HomeController.scala:32)
        at akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$2(FutureDirectives.scala:37)
        at akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$3(FutureDirectives.scala:37)
        at akka.http.scaladsl.util.FastFuture$.$anonfun$transformWith$1(FastFuture.scala:36)
        at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:40)
        at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:44)
        at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:36)
        at akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$2(FutureDirectives.scala:37)
        at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
        at akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$3(FutureDirectives.scala:37)
        at akka.http.scaladsl.util.FastFuture$.$anonfun$transformWith$1(FastFuture.scala:36)
        at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:40)
        at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:44)
        at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:36)
        at akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$2(FutureDirectives.scala:37)
        at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
        at akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$3(FutureDirectives.scala:37)
        at akka.http.scaladsl.util.FastFuture$.$anonfun$transformWith$1(FastFuture.scala:36)
        at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:40)
        at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:44)
        at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:36)
        at akka.http.scaladsl.server.directives.FutureDirectives.$anonfun$onComplete$2(FutureDirectives.scala:37)
        at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
        at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$mapRequestContext$2(BasicDirectives.scala:45)
        at akka.http.scaladsl.server.directives.BasicDirectives.$anonfun$textract$2(BasicDirectives.scala:161)
        at akka.http.scaladsl.server.RouteConcatenation$RouteWithConcatenation.$anonfun$$tilde$2(RouteConcatenation.scala:47)
        at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:40)
        at akka.http.scaladsl.util.FastFuture$.transformWith$extension(FastFuture.scala:44)
        at akka.http.scaladsl.util.FastFuture$.flatMap$extension(FastFuture.scala:25)       
"[There was an internal server error.]" did not equal "[Data is Inserted]"
    ScalaTestFailureLocation: HomeControllerSpec at (HomeControllerSpec.scala:20)
    Expected :"[Data is Inserted]"
    Actual   :"[There was an internal server error.]"
    <Click to see difference>
    
org.scalatest.exceptions.TestFailedException: "[There was an internal server error.]" did not equal "[Data is Inserted]"
        at org.scalatest.matchers.MatchersHelper$.indicateFailure(MatchersHelper.scala:344)
        at org.scalatest.matchers.should.Matchers$AnyShouldWrapper.shouldEqual(Matchers.scala:6860)
        at HomeControllerSpec.$anonfun$new$5(HomeControllerSpec.scala:20)
        at scala.util.DynamicVariable.withValue(DynamicVariable.scala:59)
        at akka.http.scaladsl.testkit.RouteTest.$anonfun$check$1(RouteTest.scala:59)
        at ......
object HomeController extends App with Directives {

  implicit val system: ActorSystem = ActorSystem()
  implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  val db = Database.forConfig("mysql")
  val obj: EmployeeRepImpl = new EmployeeRepImpl(db)

  //def router = route
  def route =
    get {
      concat(
        pathSingleSlash {
          complete {
            "Captain on the bridge!"
          }
        },
        path("insert") {
          parameters("empid", "name", "dept") { (empid, name, dept) =>
            onComplete(obj.add(empid, name, dept)) {
              case Success(value) => complete(s"Data is Inserted")
              case Failure(ex) => complete(InternalServerError, s"An error occurred: ")
            }
          }
        }
      )
    }

  val bindingFuture = Http().newServerAt("localhost", 8080).bind(route)
  println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
  StdIn.readLine() // let it run until user presses return
  bindingFuture
    .flatMap(_.unbind()) // trigger unbinding from the port
    .onComplete(_ => system.terminate())
}
trait EmployeeRep {
  def add(id: String, name: String, dept: String): Future[Unit]
  }

class EmployeeRepImpl(db: Database) extends EmployeeRep with EmployeeStore {  

  override def add(id: String, name: String, dept: String): Future[Unit] = {
    val freshTestData: DBIOAction[Unit, NoStream, Effect.Write] = DBIO.seq(
      emp ++= Seq(Employee(id, name, dept))
    )
    db.run(
      freshTestData
    )
  }
}
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import akka.http.scaladsl.model.StatusCodes
import controller.HomeController

class HomeControllerSpec extends AnyWordSpec with Matchers  with ScalatestRouteTest {


  "A Router" should {
    "return Captain on the bridge!" in {
      Get() ~> HomeController.route ~> check {
        status shouldBe StatusCodes.OK
        responseAs[String] shouldBe "Captain on the bridge!"
      }
    }
    "return a 'Data is Inserted' response for GET requests to /Insert" in {
      Get("/insert?empid=1&name=varsha&dept=de") ~> HomeController.route ~> check {
        //status shouldBe StatusCodes.OK
        responseAs[String] shouldEqual "Data is Inserted"
      }
    }
  }
}