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

Swallowed illegal access exceptions make it harder to debug modularity-related issues #2266

Closed
A248 opened this issue Apr 14, 2021 · 0 comments · Fixed by #2267
Closed

Swallowed illegal access exceptions make it harder to debug modularity-related issues #2266

A248 opened this issue Apr 14, 2021 · 0 comments · Fixed by #2267

Comments

@A248
Copy link
Contributor

A248 commented Apr 14, 2021

Issue Summary

In FieldReader and ReflectionMemberAccessor, if an IllegalAccessException is thrown, it will be suppressed.

At the very least, Mockito should include the original exception. When testing a modular application, it is common to have packages encapsulated by default.

Stacktrace

In FieldReader, the IllegalAccessException is caught. The exception is re-thrown but the cause is lost.

public Object read() {
try {
return accessor.get(field, target);
} catch (Exception e) {
throw new MockitoException(
"Cannot read state from field: " + field + ", on instance: " + target);
}
}

Using a debugger, the IllegalAccessException can be observed: https://pastebin.com/GsnU29QV

Problematic Code

  1. Use module-info in the project and don't open the package under test to mockito.

  2. Use the MockitoExtension for JUnit:

@ExtendWith(MockitoExtension.class)
public class MyClassTest {

  private final MyType mock;

  public MyClassTest(@Mock MyType mock) {
    this.mock = mock;
  }
}

Environment

Mockito-Junit-Jupiter 3.9.0
Zulu16.28+11-CA (build 16+36)

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.

1 participant