Giter Club home page Giter Club logo

Comments (8)

ultrasecreth avatar ultrasecreth commented on July 20, 2024 1

@gursahibsahni There are quite a few things wrong with this, let's separate them in 2

  1. The project you uploaded to GitHub fails because maven resolves the wrong artifacts all around, similar to this bug, I had to manually add mockito-core and bytebuddy to make it run.
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.25.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.9.7</version>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy-agent</artifactId>
            <version>1.9.7</version>
        </dependency>
  1. The test you wrote has some issues
  • The cause of the exception you posted is that you can't create a mock while you are verifying (line 57), I guess you probably want something like
Mockito.verify(headerParamsResolverMock).resolve(any[NativeWebRequest])
  • HeaderParamsResolver is being mocked properly, the fact that it looks like the real thing in the debugger is due to how the inline mocker works
  • Line 50 should stub false (I guess this is a typo as in the code above it does that)
  • The last stubbing is never going to work as nothing is gonna be equal to a mock you create and dispose, again I guess you want
doReturn("string", Nil: _*).when(headerParamsResolverMock).resolve(any[NativeWebRequest])
  • You still use pretty much the Java Mockito everywhere, if you were to use mockito-scala, your test would look more like
  "resolverArgument" should {
    "invoke resolve method if header parameter is not resolved" in {
      val parameter = mock[Parameter]

      doReturn(parameter).when(methodParameterMock).getParameter
      doReturn("string").when(parameter).getName
      doReturn(false).when(headerParamsResolverMock).isResolved
      doReturn("string").when(headerParamsResolverMock).resolve(*)
      
      irsArgumentResolver.resolveArgument(methodParameterMock, mock[ModelAndViewContainer], mock[NativeWebRequest], mock[WebDataBinderFactory])

      verify(headerParamsResolverMock).resolve(*)
    }
  }

from mockito-scala.

ultrasecreth avatar ultrasecreth commented on July 20, 2024

@gursahibsahni Hi there, 2 things

  1. Could you create a tiny GitHub project so I can clone and reproduce the issue?
  2. Looking at your StackTrace and code it seems to me you are not using anything from mockito-scala (you are using MockitoSugar from Scalatest and the Mockito object from the core Java lib), would you mind switching to it and try? your issue may be one of the many already solved in the lib.

Thanks

from mockito-scala.

gursahibsahni avatar gursahibsahni commented on July 20, 2024

@bbonanno sorry about the confusion.

I have replaced MockitoSugar of Scalatest to Mockito lib. Also, I have created a GIT repo here: https://github.com/gursahibsahni/debug-mockito-scala

The exception has changed and its a missing dependency

*** RUN ABORTED *** java.lang.NoClassDefFoundError: org/mockito/internal/invocation/InterceptedInvocation at org.mockito.internal.handler.ScalaMockHandler.handle(ScalaMockHandler.scala:23) at org.mockito.internal.handler.ScalaNullResultGuardian.handle(ScalaNullResultGuardian.scala:10) at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:35) at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:65) at org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handle(MockMethodAdvice.java:107)

from mockito-scala.

gursahibsahni avatar gursahibsahni commented on July 20, 2024

@bbonanno big thanks for addressing all the points. Especially the incorrect maven resolver.

All looks good and it feels so good writing verbose but less code for unit tests.
One thing I would bring up is that the shouldBe matcher is not implicit from the docs that its inherited from org.scalatest.Matchers. If you would like to have it on the README.md section of it, I can update it. Let me know your thoughts 👍

from mockito-scala.

ultrasecreth avatar ultrasecreth commented on July 20, 2024

@gursahibsahni No problem

You mean the examples in the tests that use shouldBe?
If that I thought it was pretty obvious as most of the examples are written in Scalatest, but yeah, it would be nicer to actually use something agnostic of the test framework (as I'm soon adding support for Specs2), if you fancy raise a pull request on that

from mockito-scala.

gursahibsahni avatar gursahibsahni commented on July 20, 2024

@bbonanno I tried to raise a PR against release/1.x but I was denied permission to push. I couldn't find the contribution guidelines I can refer to.

from mockito-scala.

ultrasecreth avatar ultrasecreth commented on July 20, 2024

@gursahibsahni Is the standard flow, you have to fork the repo, push into that fork and raise a pull request from there to this repo.

from mockito-scala.

gursahibsahni avatar gursahibsahni commented on July 20, 2024

thanks for the instructions

from mockito-scala.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.