mockito / mockito-scala

Mockito for Scala language
http://mockito.org
MIT License
354 stars 59 forks source link

Mocktio sugar failing to create mock for restclient #81

Closed arulnambi closed 5 years ago

arulnambi commented 5 years ago

Hi, I am trying to mock a library called restclient. When I try to use the mocktio sugar syntax mock(RestClient), the test fails with the following message

[ERROR] C:\work\aeegerrit\artisan\src\test\scala\org\onap\aai\mosaic\query\QueryTest.scala:20: error: class org.onap.aai.restclient.client.RestClient is not a value [ERROR] val stubbedRestClient: RestClient = mock(RestClient)

After going though the issues, I found this issue https://github.com/mockito/mockito-scala/issues/45 and tried using val stubbedRestClient: RestClient = Mockito.mock(classOf[RestClient]) and I was able to create the mock object and use it.

I am not sure if this is an issue with the restLibrary that I am using or the version of mockito-scala library

More Information mockito-scala version: mockito-scala_2.12: 1.1.4 RestClient library : https://gerrit.onap.org/r/#/admin/projects/aai/rest-client Class that I am trying to mock: https://gerrit.onap.org/r/gitweb?p=aai/rest-client.git;a=blob;f=src/main/java/org/onap/aai/restclient/client/RestClient.java;h=89d5c62d9596c147e9bba5bdfa2ec4cd0151b477;hb=refs/heads/master

BaseSpec which imports MockitoSugar that I extend into all my tests

import java.io.FileInputStream import org.junit.runner.RunWith import org.scalatest.junit.JUnitRunner import org.scalatest._ import org.mockito.{MockitoSugar, ArgumentMatchersSugar}

@RunWith(classOf[JUnitRunner]) abstract class BaseTestSpec extends FlatSpec with Matchers with OptionValues with Inside with Inspectors with TryValues with MockitoSugar with ArgumentMatchersSugar

Thanks for the lovely mocking library. This helped me to mock overloaded java functions which scalamock has issues with.

Any help is greatly appreciated.

ultrasecreth commented 5 years ago

You have to use square brackets when you specify a type in Scala, so mock[RestClient] rather than mock(RestClient)

arulnambi commented 5 years ago

Thanks for the quick response. I did try that as well, that is where I got the [ERROR] initializationError(org.onap.aai.mosaic.query.QueryTest) Time elapsed: 0.006 s <<< ERROR! scala.reflect.internal.Symbols$CyclicReference: illegal cyclic reference involving class WebResource at org.onap.aai.mosaic.query.QueryTest.(QueryTest.scala:20) That is why I was able to use the workaround you suggested in this old issue, https://github.com/mockito/mockito-scala/issues/45

ultrasecreth commented 5 years ago

@arulnambi Ok, I need to test it with that specific class to reproduce, but I can't find any artifact in maven nor I can build the clone of that repo you mentioned as it requires other dependencies from the same org, can you provide a nexus (or anything like that) link so I can add the lib to my project and try to write a test that mocks that class?

ultrasecreth commented 5 years ago

@arulnambi I managed to get the library from https://nexus.onap.org/content/groups/public/

I've used "org.onap.aai" % "rest-client" % "1.4.0"

I've written the following test and it works in all 3 major versions of scala

"mock a rest client" in {
      val stubbedRestClient: RestClient = mock[RestClient]

      stubbedRestClient.clientCertFile(*) shouldReturn stubbedRestClient

      stubbedRestClient.clientCertFile("hola") shouldBe stubbedRestClient
    }

Now, looking again at the error, it says that the problem is the class WebResource in org.onap.aai.mosaic.query.QueryTest.(QueryTest.scala:20)

I don't see that class being use by RestClient nor seems to be part of the library, could you please double check where exactly the exception is being thrown?

arulnambi commented 5 years ago

Yes that is the right library. I am not sure then why I see that error. I am using mvn and spring boot for my project can that be a cause?
Here is the full stack trace when I run mvn test witn X falg [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.295 s <<< FAILURE! - in org.onap.aai.mosaic.query.QueryTest [ERROR] initializationError(org.onap.aai.mosaic.query.QueryTest) Time elapsed: 0.006 s <<< ERROR! scala.reflect.internal.Symbols$CyclicReference: illegal cyclic reference involving class WebResource at org.onap.aai.mosaic.query.QueryTest.(QueryTest.scala:20)

[INFO] Running org.onap.aai.mosaic.query.TransformTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s - in org.onap.aai.mosaic.query.TransformTest [INFO] [INFO] Results: [INFO] [ERROR] Errors: [ERROR] QueryTest.:20->BaseTestSpec.mock:12->BaseTestSpec.mock:12 » CyclicReference [INFO] [ERROR] Tests run: 21, Failures: 0, Errors: 1, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:24 min [INFO] Finished at: 2019-02-24T08:13:14-05:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project mosaic: There are test failures. [ERROR] [ERROR] Please refer to C:\work\aeegerrit\artisan\target\surefire-reports for the individual test results. [ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. [ERROR] -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project mosaic: There are test failures.

Please refer to C:\work\aeegerrit\artisan\target\surefire-reports for the individual test results. Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288) at org.apache.maven.cli.MavenCli.main (MavenCli.java:192) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356) Caused by: org.apache.maven.plugin.MojoFailureException: There are test failures.

Please refer to C:\work\aeegerrit\artisan\target\surefire-reports for the individual test results. Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream. at org.apache.maven.plugin.surefire.SurefireHelper.throwException (SurefireHelper.java:289) at org.apache.maven.plugin.surefire.SurefireHelper.reportExecution (SurefireHelper.java:161) at org.apache.maven.plugin.surefire.SurefirePlugin.handleSummary (SurefirePlugin.java:364) at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked (AbstractSurefireMojo.java:1041) at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute (AbstractSurefireMojo.java:857) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288) at org.apache.maven.cli.MavenCli.main (MavenCli.java:192) at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:498) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356) [ERROR] [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

This is all I see @ line 20 we initialize the stubb. Thanks a lot for looking into this

ultrasecreth commented 5 years ago

Hmm, it seems to be related to a Scala compiler bug, let's try to disect it, can you, in a blank class, execute something like this and see if you get a valid WeakTypeTag?

        import scala.reflect.runtime.universe.weakTypeOf
        val tpe = weakTypeOf[RestClient]
arulnambi commented 5 years ago

Hi Bruno, I was able create the stub by using weakTypeOf. I tried passed the stubbed val for creating another object. which takes in the restclient as a parameter in its constructor val query = new Query(stubbedRestClient)

And this is what I got

[DEBUG] plugin jar to add :C:\Users\arulna.m2\repository\net\alchim31\maven\scala-maven-plugin\3.2.0\scala-maven-plugin-3.2.0.jar [DEBUG] cmd: C:\Program Files\Java\jdk1.8.0_201\jre\bin\java -Xbootclasspath/a:C:\Users\arulna.m2\repository\org\scala-lang\scala-library\2.12.7\scala-library-2.12.7.jar;C:\Users\arulna.m2\repository\org\scala-lang\scala-compiler\2.12.7\scala-compiler-2.12.7.jar;C:\Users\arulna.m2\repository\org\scala-lang\modules\scala-xml_2.12\1.0.6\scala-xml_2.12-1.0.6.jar;C:\Users\arulna.m2\repository\org\scala-lang\scala-library\2.12.0\scala-library-2.12.0.jar;C:\Users\arulna.m2\repository\org\scala-lang\scala-reflect\2.12.7\scala-reflect-2.12.7.jar -Xms512m -Xmx2048m -classpath C:\Users\arulna.m2\repository\net\alchim31\maven\scala-maven-plugin\3.2.0\scala-maven-plugin-3.2.0.jar scala_maven_executions.MainWithArgsInFile scala.tools.nsc.Main C:\Users\arulna\AppData\Local\Temp\scala-maven-8377907191515179190.args [ERROR] C:\work\aeegerrit\artisan\src\test\scala\org\onap\aai\mosaic\query\QueryTest.scala:22: error: type mismatch; [ERROR] found : reflect.runtime.universe.Type [ERROR] required: org.onap.aai.restclient.client.RestClient [ERROR] val query = new Query(stubbedRestClient) [ERROR] ^ [ERROR] C:\work\aeegerrit\artisan\src\test\scala\org\onap\aai\mosaic\query\QueryTest.scala:79: error: value get is not a member of reflect.runtime.universe.Type [ERROR] when(stubbedRestClient.get(, , *)) thenReturn getOPResult [ERROR] ^ [ERROR] two errors found

The first error was when I pass in the stubb as a argument for constructor and the second when I try to use the stubb to test a get call.

ultrasecreth commented 5 years ago

weakTypeOf does not create a mock, is a call to the Scala reflection API, so those errors make sense could you try the same but with typeOf instead of weakTypeOf? (I'm trying to find out if it is an issue on how the library does reflection or just a plain compiler bug)

I'm running out of options without being able to reproduce the issue, but, given that the class you are trying to mock is a Java one, you could use Mockito.mock(classOf[RestClient]) as a workaround

Depending on how the typeOf test do we can try something else if you like

Please let me know how it goes

ultrasecreth commented 5 years ago

@arulnambi Please try version 1.1.5 (it should be available in maven central any time now), it may fix your issue

ultrasecreth commented 5 years ago

@arulnambi can we close this issue?

arulnambi commented 5 years ago

Yes let us close it please. thanks a lot for your support and help. regards Arul

On Thu, Mar 28, 2019 at 5:35 PM Bruno Bonanno notifications@github.com wrote:

@arulnambi https://github.com/arulnambi can we close this issue?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mockito/mockito-scala/issues/81#issuecomment-477780169, or mute the thread https://github.com/notifications/unsubscribe-auth/ADa8mxpbV0jggEV0f1czNiyxXrH8CgV1ks5vbTWwgaJpZM4bJ2wj .

ultrasecreth commented 5 years ago

@arulnambi no problem!