forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes mockito#2905 : ThreadLocal classes can be mocked.
- Loading branch information
Roberto Trunfio
authored and
Roberto Trunfio
committed
Feb 8, 2023
1 parent
10515bc
commit 822f359
Showing
2 changed files
with
61 additions
and
1 deletion.
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
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,60 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitousage.bugs; | ||
|
||
import static org.mockito.Mockito.RETURNS_MOCKS; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockitoutil.TestBase; | ||
|
||
/** | ||
* This was an issue reported in #2905. Mocking {@link ThreadLocal} or classes extending {@link ThreadLocal} was | ||
* throwing a {@link StackOverflowError}. | ||
*/ | ||
public class ThreadLocalTest extends TestBase { | ||
|
||
@Test | ||
public void mock_ThreadLocal_does_not_raise_StackOverflowError() { | ||
StackOverflowError stackOverflowError = Assertions.catchThrowableOfType(() -> { | ||
mock(ThreadLocal.class, RETURNS_MOCKS); | ||
}, StackOverflowError.class); | ||
Assertions.assertThat(stackOverflowError).isNull(); | ||
} | ||
|
||
@Test | ||
public void mock_class_extending_ThreadLocal_does_not_raise_StackOverflowError() { | ||
StackOverflowError stackOverflowError = Assertions.catchThrowableOfType(() -> { | ||
mock(SomeThreadLocal.class, RETURNS_MOCKS); | ||
}, StackOverflowError.class); | ||
Assertions.assertThat(stackOverflowError).isNull(); | ||
} | ||
|
||
@Test | ||
public void spy_ThreadLocal_does_not_raise_StackOverflowError() { | ||
StackOverflowError stackOverflowError = Assertions.catchThrowableOfType(() -> { | ||
spy(ThreadLocal.class); | ||
}, StackOverflowError.class); | ||
Assertions.assertThat(stackOverflowError).isNull(); | ||
} | ||
|
||
@Test | ||
public void spy_class_extending_ThreadLocal_does_not_raise_StackOverflowError() { | ||
StackOverflowError stackOverflowError = Assertions.catchThrowableOfType(() -> { | ||
spy(SomeThreadLocal.class); | ||
}, StackOverflowError.class); | ||
Assertions.assertThat(stackOverflowError).isNull(); | ||
} | ||
|
||
static class SomeThreadLocal<T> extends ThreadLocal<T> { | ||
|
||
} | ||
|
||
} | ||
|