问题描述
下面给出错误
[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 ......
我的代码=> ScalaRoutes =>
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,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())
}
EmployeeRep =>
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,dept: String): Future[Unit] = {
val freshTestData: DBIOAction[Unit,NoStream,Effect.Write] = DBIO.seq(
emp ++= Seq(Employee(id,dept))
)
db.run(
freshTestData
)
}
}
HomeControllSpec
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"
}
}
}
}
无法理解为什么第二条路由会出现内部服务器错误/ insert尝试了上面的代码,但未能成功 / insert URL将命中数据库并将数据插入表中。但是当我尝试进行单元测试时,它给了我上面的错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)