| 1 |
/* |
| 2 |
* Copyright (c) 2017 Mockito contributors |
| 3 |
* This program is made available under the terms of the MIT License. |
| 4 |
*/ |
| 5 |
package org.mockitoinline; |
| 6 |
|
| 7 |
import java.util.Collections; |
| 8 |
import java.util.Set; |
| 9 |
import org.junit.Test; |
| 10 |
import org.mockito.internal.invocation.finder.AllInvocationsFinder; |
| 11 |
import org.mockito.stubbing.Stubbing; |
| 12 |
|
| 13 |
import static org.junit.Assert.assertEquals; |
| 14 |
import static org.mockito.Mockito.mock; |
| 15 |
import static org.mockito.Mockito.when; |
| 16 |
|
| 17 |
public class StubbingLocationTest { |
| 18 |
|
| 19 |
@Test |
| 20 |
public void stubbing_location_should_be_the_correct_point() { |
| 21 |
ConcreteClass mock = mock(ConcreteClass.class); |
| 22 |
String frame; |
| 23 |
// Initializing 'frame' at the method parameter point is to gain the exact line number of the stubbing point. |
| 24 |
when(mock.concreteMethod(frame = Thread.currentThread().getStackTrace()[1].toString())).thenReturn(""); |
| 25 |
mock.concreteMethod(frame); |
| 26 |
Set<Stubbing> stubbings = AllInvocationsFinder.findStubbings(Collections.singleton(mock)); |
| 27 |
assertEquals(1, stubbings.size()); |
| 28 |
String location = stubbings.iterator().next().getInvocation().getLocation().toString(); |
| 29 |
assertEquals("-> at " + frame, location); |
| 30 |
} |
| 31 |
|
| 32 |
static final class ConcreteClass { |
| 33 |
String concreteMethod(String s) { |
| 34 |
throw new RuntimeException(s); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
} |