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

Prefer non-synthetic constructors in MockMethodAdvice.ConstructorShortcut #2045

Closed
wants to merge 1 commit into from

Conversation

hoisie
Copy link

@hoisie hoisie commented Sep 16, 2020

After moving spy creation to instrumenting constructor chains in
ByteBuddyMockMaker, creating spies for Robolectric-instrumented Android
classes started failing in some situations due to fields not being
copied on base classes (specifically, ContextWrapper.mBase). The problem
is that AsmVisitorWrapper.ForDeclaredMethods does not visit the
synthetic constructrs that are added by Robolectric during runtime.
While the visibility issuer is still being explored, a workaround is to
prefer non-synthetic constructors when selecting which parent
constructor to call in MockMethodAdvice.ConstructorShortcut.

Fixes #2040

@hoisie hoisie changed the title Prefer non-synthetic constructors in MockMethodAdvice.ConstructorShottcut Prefer non-synthetic constructors in MockMethodAdvice.ConstructorShortcut Sep 16, 2020
…tcut

After moving spy creation to instrumenting constructor chains in
ByteBuddyMockMaker, creating spies for Robolectric-instrumented Android
classes started failing in some situations due to fields not being
copied on base classes (specifically, ContextWrapper.mBase). The problem
is that AsmVisitorWrapper.ForDeclaredMethods does not visit the
synthetic constructrs that are added by Robolectric during runtime.
While the visibility issuer is still being explored, a workaround is to
prefer non-synthetic constructors when selecting which parent
constructor to call in MockMethodAdvice.ConstructorShortcut.

Fixes mockito#2040
@codecov-commenter
Copy link

codecov-commenter commented Sep 16, 2020

Codecov Report

Merging #2045 into release/3.x will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@              Coverage Diff               @@
##             release/3.x    #2045   +/-   ##
==============================================
  Coverage          84.89%   84.90%           
  Complexity          2704     2704           
==============================================
  Files                325      325           
  Lines               8204     8208    +4     
  Branches             979      980    +1     
==============================================
+ Hits                6965     6969    +4     
  Misses               968      968           
  Partials             271      271           
Impacted Files Coverage Δ Complexity Δ
.../internal/creation/bytebuddy/MockMethodAdvice.java 78.09% <100.00%> (+0.25%) 23.00 <0.00> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b6ae6cf...6c220a1. Read the comment docs.

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get a regression test as well? Not sure if it is worth it though.

@raphw
Copy link
Member

raphw commented Sep 16, 2020

I hope it's temporary and we get to fix the root problem some time in the nesr future.

@hoisie
Copy link
Author

hoisie commented Sep 16, 2020

Could we get a regression test as well? Not sure if it is worth it though.

I can try, though it would essentially recreating a Robolectric-like environment, so it may be challenging, considering there are so many moving parts to a Robolectric environment. I'll could try to create a custom child-first classloader that instuments a class to insert a constructor and see if I can replicate the failure.

FWIW I did add a regression test on the Robolectric side: https://github.com/robolectric/robolectric/blob/master/integration_tests/mockito-experimental/src/test/java/org/robolectric/integrationtests/mockito/experimental/MockitoSpyTest.java

@TimvdLippe
Copy link
Contributor

Let's keep the test on the Robolectric side, that makes sense to me. Thanks!

@raphw
Copy link
Member

raphw commented Sep 16, 2020

I found the actual problem: #2046

We excluded synthetic constructors by Byte Buddy's default. facepalm

@raphw raphw closed this Sep 16, 2020
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 this pull request may close these issues.

Mockito.spy(Activity).getBaseContext() returns null on Robolectric 4.4 and Java8
4 participants