Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sa- opened this issue Jan 27, 2021 · 14 comments · Fixed by #354
Open

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

sa- opened this issue Jan 27, 2021 · 14 comments · Fixed by #354

Comments

@sa-
Copy link

sa- commented Jan 27, 2021

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- sa- changed the title java.lang.NoSuchFieldException: modifiers withObjectMocked leads to "java.lang.NoSuchFieldException: modifiers" Jan 27, 2021
@ultrasecreth
Copy link
Member

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?

@trompa
Copy link
Contributor

trompa commented Feb 2, 2021

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

@ultrasecreth
Copy link
Member

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

@trompa
Copy link
Contributor

trompa commented Feb 2, 2021

More info on the issue:
There is a change in final fields for java 13 that prevents this to work

See:
powermock/powermock#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

ultrasecreth pushed a commit that referenced this issue Feb 2, 2021
* getField for java 12+

* Fix for java 13+, avoid type conversion error

Co-authored-by: Alberto Lago <[email protected]>
@ultrasecreth
Copy link
Member

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

@trompa
Copy link
Contributor

trompa commented Feb 2, 2021

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

@ultrasecreth
Copy link
Member

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 pushed a commit that referenced this issue Feb 2, 2021
* getField for java 12+

* Fix for java 13+, avoid type conversion error

* Fix the right method

* undo allchanges

* undo allchanges

* remove unused import

* sync upstream and apply changes

Co-authored-by: Alberto Lago <[email protected]>

[skip ci]
@thespags
Copy link

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.

@ultrasecreth
Copy link
Member

Will have a look

@ultrasecreth ultrasecreth reopened this Oct 21, 2021
@kurtrwall
Copy link

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

@mazers0129
Copy link

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

@raucher
Copy link

raucher commented Sep 11, 2023

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

@eanea
Copy link

eanea commented Oct 19, 2023

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

@txia1989
Copy link

txia1989 commented Feb 8, 2024

Same issue
JDK 17
Scala 2.12.12
Mockito-scala 1.17.29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants