| 1 |
/* |
| 2 |
* Copyright (c) 2018 Mockito contributors |
| 3 |
* This program is made available under the terms of the MIT License. |
| 4 |
*/ |
| 5 |
package org.mockitoinline; |
| 6 |
|
| 7 |
import org.junit.Test; |
| 8 |
|
| 9 |
import static org.junit.Assert.assertEquals; |
| 10 |
import static org.mockito.Mockito.mock; |
| 11 |
import static org.mockito.Mockito.when; |
| 12 |
|
| 13 |
public class OneLinerStubStressTest { |
| 14 |
|
| 15 |
public class OneLinerStubTestClass { |
| 16 |
public String getStuff() { |
| 17 |
return "A"; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
private static String generateLargeString() { |
| 22 |
final int length = 2000000; |
| 23 |
final StringBuilder stringBuilder = new StringBuilder(length); |
| 24 |
for (int i = 0; i <= length; i++) { |
| 25 |
stringBuilder.append("B"); |
| 26 |
} |
| 27 |
return stringBuilder.toString(); |
| 28 |
} |
| 29 |
|
| 30 |
@Test |
| 31 |
public void call_a_lot_of_mocks_using_one_line_stubbing() { |
| 32 |
//This requires smaller heap set for the test process, see "inline.gradle" |
| 33 |
final String returnValue = generateLargeString(); |
| 34 |
for (int i = 0; i < 50000; i++) { |
| 35 |
// make sure that mock object does not get cleaned up prematurely |
| 36 |
final OneLinerStubTestClass mock = |
| 37 |
when(mock(OneLinerStubTestClass.class).getStuff()).thenReturn(returnValue).getMock(); |
| 38 |
assertEquals(returnValue, mock.getStuff()); |
| 39 |
} |
| 40 |
} |
| 41 |
} |