question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpExt.singleRequest with implicit parameters can not be mocked

See original GitHub issue

If I try to mock the object HttpExt (imported like this import akka.http.scaladsl.HttpExt) it will not work with this library. This one will mock the object

import akka.http.scaladsl.HttpExt
import org.scalatest.mockito.MockitoSugar.mock
...

val mockHttp = mock[HttpExt]

this one will not work i.e. it will just return an instance of HttpExt and not a mock

import akka.http.scaladsl.HttpExt
import org.mockito.MockitoSugar.mock

...

val mockHttp = mock[HttpExt]

I also could not spy an HttpExt instance.

I am using

"org.mockito" % "mockito-scala_2.12" % "1.0.1"

and


"org.scalatest" % "scalatest_2.12" % "3.0.5" % "test"

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bbonannocommented, Nov 19, 2018

@gadcam It actually doesn’t For starters, Scalatest doesn’t provide any mocking library, it only wraps the mock method with a nicer syntax, but you are 100% using the Java Mockito. Java Mockito is completely oblivious to default arguments (and any other Scala-only feature), from the Mockito point of view, they are null if you don’t provide them (you can check this by writing a verify to see what arguments were passed to the method). You can find a more detailed explanation here

2reactions
bbonannocommented, Nov 16, 2018

@gadcam The problem is that mockito-scala is aware of default arguments, if you look at the signature of singleRequest it looks like

  def singleRequest(
    request:           HttpRequest,
    connectionContext: HttpsConnectionContext = defaultClientHttpsContext,
    settings:          ConnectionPoolSettings = defaultConnectionPoolSettings,
    log:               LoggingAdapter         = system.log): Future[HttpResponse]

Now, the log parameter receives as default system.log. As system hasn’t been stubbed, mockito-scala will return a SmartNull (a mock that will fail if you call any method on it) which is the one that is throwing that exception you see…

You have many ways to overcome this, but probably the easiest is to pass a mock of a LoggingAdapter to the singleRequest method so the default value is not used…

BTW, your code would look much cleaner with the new API, e.g.

  val randomRequest = HttpRequest()
  val response      = Future(HttpResponse())
  "mock" should {
    "should work if coming from mockito" in {
      val mockHttp = mock[HttpExt]
      val logger   = mock[LoggingAdapter]

      mockHttp.singleRequest(*, *, *, *) shouldReturn response

      mockHttp.singleRequest(randomRequest, log = logger) should be theSameInstanceAs response
    }
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock a method without arguments but with implicit parameters
The solution is: (m.getResult()(_: ExecutionContext)).expects(*) returning "...", if you update your answer I will accept it.
Read more >
[Solved]-How to test client-side Akka HTTP-scala
singleRequest (_) def sampleTextFile(uri: Uri)( implicit akkaSystem: ActorSystem, ... Then in my test I can just provide a mock HttpResponder .
Read more >
Implicit Parameters - Oracle Help Center
This chapter describes the implicit parameters used in REST service handlers that are not explicitly declared. Oracle REST Data Services (ORDS) adds these ......
Read more >
How to verify mocked method call involving implicit arguments ...
Invalid use of argument matchers! 2 matchers expected, 1 recorded. This exception may occur if matchers are combined with raw values: //incorrect:
Read more >
Implicit Parameters In Scala - GeeksforGeeks
In simpler terms, if no value or parameter is passed to a method or function, then the compiler will look for implicit value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found