mockito-scala
mockito-scala copied to clipboard
withObjectMocked leads to "java.lang.NoSuchFieldException: modifiers"
I'm using java 8 and scala 2.12.
Is withObjectMocked
supported in Java 8?
[info] java.lang.NoSuchFieldException: modifiers
[info] at java.base/java.lang.Class.getDeclaredField(Class.java:2412)
[info] at org.mockito.ReflectionUtils$.setFinalStatic(ReflectionUtils.scala:128)
[info] at org.mockito.MockitoEnhancer.withObjectMocked(MockitoAPI.scala:641)
[info] at org.mockito.MockitoEnhancer.withObjectMocked$(MockitoAPI.scala:630)
Our CI tests Scala 2.11, 2.12 & 2.13 on both Java 8 & 11, so it should work... Mind posting a code snippet that can be used to reproduce the issue?
I believe that original poster is running the tests with a bigger java version than 8, this is a problem I am facing for java 14, but not when i pass java 8 to sbt through -java-home command
I guess it could be worth adding CI on Java 14 or 15 to avoid this kind of issues in the future
More info on the issue: There is a change in final fields for java 13 that prevents this to work
See: https://github.com/powermock/powermock/issues/939 Ported this change to scala and will create a PR. Local tests show it fixed the error. CI tests for java 13+ should be added
@trompa thanks a lot for the fix, will add CI on 13+ versions soon!
Unfortunately, it wasn complete, need to apply the same fix to setFinalStatic, working on it
No prob, mind adding a newer Java version here? https://github.com/mockito/mockito-scala/blob/release/1.x/.github/workflows/ci.yml this way we know all of these changes work for sure :)
I'm still seeing this issue with java8, scala 2.11, and mockito-scala-scalatest 1.16.46.
modifiers
java.lang.NoSuchFieldException: modifiers
I'm using the code from the example in the readme
def simpleMethod: String = "not mocked!"
}
class Foo extends AnyFunSuite with IdiomaticMockito {
test("mock") {
FooObject.simpleMethod shouldBe "not mocked!"
withObjectMocked[FooObject.type] {
FooObject.simpleMethod returns "mocked!"
//or
when(FooObject.simpleMethod) thenReturn "mocked!"
FooObject.simpleMethod shouldBe "mocked!"
}
FooObject.simpleMethod shouldBe "not mocked!"
}
}
Thank you.
Will have a look
I'm seeing this using Java 8, Scala 2.12, mockito v1.17.0.
temporary... I add vm option to my test. --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED
it work for me!!
- ENV - jdk17 scala 2.6.19 mockito-scala-scalatest 1.17.7
It's very interesting, but https://github.com/mockito/mockito-scala#mocking-scala-object says "Since version 1.16.0 it is possible to mock object methods, given that such definitions are global" I've tried to declare an object at most-outer scope (neither in a class, nor in a method) and it works.
object FooObject {
def foo: String = "not mocked"
}
class ApiConsumerActorTest extends TestKit(ActorSystem("ApiConsumerActorTest"))
{ ... }
If object is declared inside a class or method, run fails with exception :/
JDK: 1.8.0_382 Scala: 2.12.18 Mockito-scala: 1.17.12
facing the same issue. JDK 17 Scala 2.13 Mockito-scala 1.17.27
seems related: https://stackoverflow.com/questions/56039341/get-declared-fields-of-java-lang-reflect-fields-in-jdk12
Same issue JDK 17 Scala 2.12.12 Mockito-scala 1.17.29