New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the client mockable with Java mocking frameworks #108
Comments
The other option is to add a mock implementation directly if possible. |
@wsargent Could you please illustrate this a bit more? Do you mean based on the |
Basically -- taking play-mockws and merging it into play-ws. Then the release cycle problem goes away. |
This would be an option. But is it worth to implement a mock implementation instead of using types based on generics? I don't know the problems you had with the generic implementation. Does it generally not work or is it only ugly? |
@wsargent I need to know if there is a solution for the final Play 2.6 release, so that I can proceed with my tasks. A short info would be great! |
Hi @akkie can you provide a small self-contained project as described in https://groups.google.com/d/msg/play-framework/mYPRPbfa8Uk/DFl3ONJcAgAJ ? |
@akkie This unit test seems to work fine: package play.api.libs.ws
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import scala.concurrent.Future
class MockitoSpec extends Specification with Mockito {
trait WSRequest extends StandaloneWSRequest {
type Self = StandaloneWSRequest
type Response = StandaloneWSResponse
}
"wsrequest" should {
"be mockable" in {
val wsRequest = mock[WSRequest]
val wsResponse = mock[StandaloneWSResponse]
wsResponse.json throws new RuntimeException("Unexpected character ('<' (code 60))")
wsResponse.body returns "<html></html>"
wsRequest.withHeaders(any) returns wsRequest
wsRequest.post[Map[String, Seq[String]]](any)(any) returns Future.successful(wsResponse)
success
}
}
}
|
@wsargent Yes it works, but normally you deal with the But, you brought me to an idea. From the beginning, I've used the HTTPLayer trait to provide a mockable implementation for the previously provided global |
Awesome! |
Just as a quick feedback. We converted our projects to Play 2.6 and the latest wsClient (lots and lots of code, lots and lots of mocks of wsClient stuff). And all worked perfectly fine. 👍 |
Thanks for the feedback, @raphaelbauer! |
Any one knows how to solve the "Unexpected character ('<' (code 60))" on wsResponse.json? |
Play WS Version (2.5.x / etc)
1.0.0-M8
API (Scala / Java / Neither / Both)
Scala
Expected Behavior
It should be able to mock the client with Java mocking libraries like mockito, EasyMock or JMock.
Actual Behavior
The following code doesn't compile:
Reproducible Test Case
mohiva/play-silhouette#509
Links
https://groups.google.com/forum/#!topic/play-framework/mYPRPbfa8Uk
Notes
Both Scala testing libraries support mocking with Java mocking frameworks. ScalaTest supports one Scala and three Java mocking frameworks. Specs2 provides syntactic sugar for mockito. This shows that using Java mocking frameworks in the Scala world is a preferred method.
Switching to play-mockws as suggested by @wsargent , isn't a good solution for a play related library, because then the library is bound to the release cycle of that play related dependency.
The text was updated successfully, but these errors were encountered: