-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mockito fails with JVM internal error when mocking class where clinit…
… does not find a transitive dependency Mockito now throws Errors occurred during class initialization to callers instead of swallowing it, and the JVM reporting an obscure exception. Fixes #3498
- Loading branch information
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...-core/src/test/java/org/mockitousage/bugs/creation/MockClassWithMissingStaticDepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2024 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.bugs.creation; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.fail; | ||
|
||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.exceptions.base.MockitoException; | ||
|
||
// See Issue #3498 | ||
public class MockClassWithMissingStaticDepTest { | ||
|
||
@Test | ||
public void shouldNotSwallowCinitErrorsOfClassToMock() { | ||
try { | ||
@SuppressWarnings("unused") | ||
var unused = Mockito.mock(ClassWithErrorInClassInit.class); | ||
|
||
fail(); | ||
} catch (MockitoException e) { | ||
var cause = e.getCause(); | ||
assertThat(cause).isInstanceOf(MockitoException.class); | ||
assertThat(cause.getMessage()) | ||
.isEqualTo( | ||
"Cannot instrument class org.mockitousage.bugs.creation.MockClassWithMissingStaticDepTest$ClassWithErrorInClassInit because it or one of its supertypes could not be initialized"); | ||
|
||
var cause2 = cause.getCause(); | ||
assertThat(cause2).isInstanceOf(NoClassDefFoundError.class); | ||
assertThat(cause2.getMessage()) | ||
.isEqualTo("Simulate missing transitive dependency used in class init."); | ||
} | ||
} | ||
|
||
private static class ClassWithErrorInClassInit { | ||
static { | ||
//noinspection ConstantValue | ||
if (true) { | ||
throw new NoClassDefFoundError( | ||
"Simulate missing transitive dependency used in class init."); | ||
} | ||
} | ||
} | ||
} |