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.

Mock creation does not work for JSch projects ChannelSftp class with Mockito 2.7.22

See original GitHub issue

Hey,

We have project with Scala and use ScalaTest for testing Framework and Mockito for mocking.

After upgrading to 2.7.22 (tested with all 2.x branches - including using tag v2.8.28), only one class cannot be mocked anymore.

import com.jcraft.jsch.ChannelSftp

val mockChannelSftp: ChannelSftp = mock(classOf[ChannelSftp])

Exception:

java.lang.IllegalAccessError: tried to access class com.jcraft.jsch.ChannelSession from class com.jcraft.jsch.ChannelSftp$MockitoMock$1916471588
	at com.jcraft.jsch.ChannelSftp$MockitoMock$1916471588.<clinit>(Unknown Source)
	at sun.reflect.GeneratedSerializationConstructorAccessor33.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
	at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
	at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:18)
	at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.createMock(SubclassByteBuddyMockMaker.java:47)
	at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createMock(ByteBuddyMockMaker.java:25)
	at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:35)
	at org.mockito.internal.MockitoCore.mock(MockitoCore.java:63)
	at org.mockito.Mockito.mock(Mockito.java:1729)
	at org.mockito.Mockito.mock(Mockito.java:1642)
	at ExampleSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(ExampleSpec.scala:10)
	at ExampleSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(ExampleSpec.scala:9)
	at ExampleSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(ExampleSpec.scala:9)
	at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
	at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
	at org.scalatest.Transformer.apply(Transformer.scala:22)
	at org.scalatest.Transformer.apply(Transformer.scala:20)
	at org.scalatest.WordSpecLike$$anon$1.apply(WordSpecLike.scala:1078)
	at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
	at org.scalatest.WordSpec.withFixture(WordSpec.scala:1881)
	at org.scalatest.WordSpecLike$class.invokeWithFixture$1(WordSpecLike.scala:1075)
	at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
	at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
	at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
	at org.scalatest.WordSpecLike$class.runTest(WordSpecLike.scala:1088)
	at org.scalatest.WordSpec.runTest(WordSpec.scala:1881)
	at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
	at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
	at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396)
	at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
	at scala.collection.immutable.List.foreach(List.scala:392)
	at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
	at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:373)
	at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:410)
	at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
	at scala.collection.immutable.List.foreach(List.scala:392)
	at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
	at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379)
	at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
	at org.scalatest.WordSpecLike$class.runTests(WordSpecLike.scala:1147)
	at org.scalatest.WordSpec.runTests(WordSpec.scala:1881)
	at org.scalatest.Suite$class.run(Suite.scala:1147)
	at org.scalatest.WordSpec.org$scalatest$WordSpecLike$$super$run(WordSpec.scala:1881)
	at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
	at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
	at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
	at org.scalatest.WordSpecLike$class.run(WordSpecLike.scala:1192)
	at org.scalatest.WordSpec.run(WordSpec.scala:1881)
	at org.scalatest.tools.Framework.org$scalatest$tools$Framework$$runSuite(Framework.scala:314)
	at org.scalatest.tools.Framework$ScalaTestTask.execute(Framework.scala:480)
	at sbt.TestRunner.runTest$1(TestFramework.scala:76)
	at sbt.TestRunner.run(TestFramework.scala:85)
	at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
	at sbt.TestFramework$$anon$2$$anonfun$$init$$1$$anonfun$apply$8.apply(TestFramework.scala:202)
	at sbt.TestFramework$.sbt$TestFramework$$withContextLoader(TestFramework.scala:185)
	at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:202)
	at sbt.TestFramework$$anon$2$$anonfun$$init$$1.apply(TestFramework.scala:202)
	at sbt.TestFunction.apply(TestFramework.scala:207)
	at sbt.Tests$$anonfun$9.apply(Tests.scala:216)
	at sbt.Tests$$anonfun$9.apply(Tests.scala:216)
	at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:44)
	at sbt.std.Transform$$anon$3$$anonfun$apply$2.apply(System.scala:44)
	at sbt.std.Transform$$anon$4.work(System.scala:63)
	at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
	at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
	at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
	at sbt.Execute.work(Execute.scala:237)
	at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
	at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
	at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
	at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

Example failing project after upgrade (online line example with dependencies)

For running test you need SBT and run it with command sbt test

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pollycchangcommented, Oct 4, 2017

I ran into this exact issue. I can only get my test to work with Mockito 1.10. One interesting thing I noticed was that IntelliJ didn’t have a problem running my test with Mockito 2.7. The problem only appeared when I ran the test using sbt (0.13.8). Is there any fix for this issue?

0reactions
margussipriacommented, Oct 2, 2019

Hello,

After long time I tried to upgrade to our Mockito to 3.1.0 (nice jump from 1.10.19) and everything worked.

Then i did get back to my https://github.com/margussipria/mockito-bug project and started testing.

Mockito had fixed this problem at version 2.24.0, tried every version from 2.16.0 (last failing version tested 2 years ago) to 2.24.0.

But I also noticed something with failing version 2.16.0, it had stared working, and only thing I did previously was to switch to SBT version 1.3.2 just to get load time performance boost.

I also tested with different SBT versions using “failing” Mockito 2.16.0 and it worked with SBT 1.3.2 and 1.2.8, but failed with 1.1.6.

So independently to Mockito change, also SBT had fixed underlying cause as @trcooke had tested with Gradle? and it had worked.

I would say that this can be closed? just upgrade Mockito to 2.24.0+ and/or SBT to 1.2.8+.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock creation does not work for JSch projects ChannelSftp ...
After upgrading to 2.7.22 (tested with all 2.x branches - including using tag v2.8.28), only one class cannot be mocked anymore. import com....
Read more >
Mockito cannot mock this class - java - Stack Overflow
I solved the problem by changing the Java version from 18 to 11. File -> Project Structure -> ...
Read more >
com.jcraft.jsch.Session Java Examples - ProgramCreek.com
This page shows Java code examples of com.jcraft.jsch.Session.
Read more >
NoSuchMethodError with createMockHandler method.
Hi,. I'm using the following versions of Mockito and PowerMockito but am having issues with unit testing some classes with regards to what...
Read more >
Java Code Examples of com.jcraft.jsch.Channel
The examples are extracted from open source Java projects from GitHub. ... @throws CoreException if exec channel can not be created, or session...
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