简体   繁体   中英

java.lang.ClassCastException: [B cannot be cast to play.twirl.api.Html

I'm taking a project and upgrading it to Play 2.5 (from 2.1). One problem I've hit is with this test:

  val actualResult : Result = ...

  val it = Iteratee.fold[Html, List[Html]](List.empty) {
      (a, b) => {
        b :: a
      }
    }

    val bodyIt = actualResult.body.asInstanceOf[Enumerator[Html]].apply(it)
    val bodyItResult = Await.result(bodyIt, 1 second)
    val bodyList = Await.result(bodyItResult.run, 1 second)
    val bodyText = bodyList.toString

    bodyText should include(callbackUrl)
  }

I get the error

java.lang.ClassCastException: [B cannot be cast to play.twirl.api.Html

Thrown in the iterator. Why can't it cast the object?

As Sergey pointed out, this was a type mismatch, and I solved it as follows:

  val actualResult : Result = ...

  val it = Iteratee.fold[Array[Byte], List[Array[Byte]]]](List.empty) {
      (a, b) => {
        b :: a
      }
    }

    val bodyIt = actualResult.body.asInstanceOf[Enumerator[Html]].apply(it)
    val bodyItResult = Await.result(bodyIt, 1 second)
    val bodyList = Await.result(bodyItResult.run, 1 second)
    val bodyBytes = bodyList.head
    val bodyText = new String(bodyBytes)
    bodyText should include(callbackUrl)
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM