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.
Merge pull request mockito#1544 from maxgrabenhorst/fix_premature_cle…
…anup Fixes mockito#1541: Prevent premature garbage collection of mock objects
- Loading branch information
Showing
5 changed files
with
74 additions
and
28 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
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
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
41 changes: 41 additions & 0 deletions
41
subprojects/inline/src/test/java/org/mockitoinline/OneLinerStubStressTest.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,41 @@ | ||
/* | ||
* Copyright (c) 2018 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitoinline; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class OneLinerStubStressTest { | ||
|
||
public class OneLinerStubTestClass { | ||
public String getStuff() { | ||
return "A"; | ||
} | ||
} | ||
|
||
private static String generateLargeString() { | ||
final int length = 2000000; | ||
final StringBuilder stringBuilder = new StringBuilder(length); | ||
for (int i = 0; i <= length; i++) { | ||
stringBuilder.append("B"); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
|
||
@Test | ||
public void call_a_lot_of_mocks_using_one_line_stubbing() { | ||
//This requires smaller heap set for the test process, see "inline.gradle" | ||
final String returnValue = generateLargeString(); | ||
for (int i = 0; i < 50000; i++) { | ||
// make sure that mock object does not get cleaned up prematurely | ||
final OneLinerStubTestClass mock = | ||
when(mock(OneLinerStubTestClass.class).getStuff()).thenReturn(returnValue).getMock(); | ||
assertEquals(returnValue, mock.getStuff()); | ||
} | ||
} | ||
} |