mockito-scala icon indicating copy to clipboard operation
mockito-scala copied to clipboard

withObjectMocked leads to "java.lang.NoSuchFieldException: modifiers"

Open sa- opened this issue 3 years ago • 14 comments

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)

sa- avatar Jan 27 '21 15:01 sa-

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?

ultrasecreth avatar Jan 27 '21 22:01 ultrasecreth

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

trompa avatar Feb 02 '21 09:02 trompa

I guess it could be worth adding CI on Java 14 or 15 to avoid this kind of issues in the future

ultrasecreth avatar Feb 02 '21 09:02 ultrasecreth

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 avatar Feb 02 '21 09:02 trompa

@trompa thanks a lot for the fix, will add CI on 13+ versions soon!

ultrasecreth avatar Feb 02 '21 09:02 ultrasecreth

Unfortunately, it wasn complete, need to apply the same fix to setFinalStatic, working on it

trompa avatar Feb 02 '21 11:02 trompa

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 :)

ultrasecreth avatar Feb 02 '21 11:02 ultrasecreth

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.

thespags avatar Oct 20 '21 23:10 thespags

Will have a look

ultrasecreth avatar Oct 21 '21 15:10 ultrasecreth

I'm seeing this using Java 8, Scala 2.12, mockito v1.17.0.

kurtrwall avatar Feb 17 '22 00:02 kurtrwall

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

mazers0129 avatar May 30 '22 02:05 mazers0129

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

raucher avatar Sep 11 '23 13:09 raucher

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

eanea avatar Oct 19 '23 09:10 eanea

Same issue JDK 17 Scala 2.12.12 Mockito-scala 1.17.29

txia1989 avatar Feb 08 '24 00:02 txia1989