use of org.mockito.stubbing.Stubbing in project mockito by mockito.
the class DefaultStubbingLookupListener method potentialArgMismatches.
private static List<Invocation> potentialArgMismatches(Invocation invocation) {
List<Invocation> matchingStubbings = new LinkedList<Invocation>();
Collection<Stubbing> stubbings = mockingDetails(invocation.getMock()).getStubbings();
for (Stubbing s : stubbings) {
if (!s.wasUsed() && s.getInvocation().getMethod().getName().equals(invocation.getMethod().getName())) {
matchingStubbings.add(s.getInvocation());
}
}
return matchingStubbings;
}
use of org.mockito.stubbing.Stubbing in project mockito by mockito.
the class UnusedStubbingsFinder method getUnusedStubbingsByLocation.
/**
* Gets unused stubbings per location. This method is less accurate than {@link #getUnusedStubbings(Iterable)}.
* It considers that stubbings with the same location (e.g. ClassFile + line number) are the same.
* This is not completely accurate because a stubbing declared in a setup or constructor
* is created per each test method. Because those are different test methods,
* different mocks are created, different 'Invocation' instance is backing the 'Stubbing' instance.
* In certain scenarios (detecting unused stubbings by JUnit runner), we need this exact level of accuracy.
* Stubbing declared in constructor but realized in % of test methods is considered as 'used' stubbing.
* There are high level unit tests that demonstrate this scenario.
*/
public Collection<Invocation> getUnusedStubbingsByLocation(Iterable<Object> mocks) {
Set<Stubbing> stubbings = AllInvocationsFinder.findStubbings(mocks);
// 1st pass, collect all the locations of the stubbings that were used
// note that those are _not_ locations where the stubbings was used
Set<String> locationsOfUsedStubbings = new HashSet<>();
for (Stubbing s : stubbings) {
if (!UnusedStubbingReporting.shouldBeReported(s)) {
String location = s.getInvocation().getLocation().toString();
locationsOfUsedStubbings.add(location);
}
}
// 2nd pass, collect unused stubbings by location
// If the location matches we assume the stubbing was used in at least one test method
// Also, using map to deduplicate reported unused stubbings
// if unused stubbing appear in the setup method / constructor we don't want to report it
// per each test case
Map<String, Invocation> out = new LinkedHashMap<>();
for (Stubbing s : stubbings) {
String location = s.getInvocation().getLocation().toString();
if (!locationsOfUsedStubbings.contains(location)) {
out.put(location, s.getInvocation());
}
}
return out.values();
}
use of org.mockito.stubbing.Stubbing in project mockito by mockito.
the class AllInvocationsFinder method findStubbings.
/**
* Gets all stubbings from mocks. Invocations are ordered earlier first.
*
* @param mocks mocks
* @return stubbings
*/
public static Set<Stubbing> findStubbings(Iterable<?> mocks) {
Set<Stubbing> stubbings = new TreeSet<>(new StubbingComparator());
for (Object mock : mocks) {
// the static mock.
if (mock instanceof Class<?>) {
continue;
}
Collection<? extends Stubbing> fromSingleMock = new DefaultMockingDetails(mock).getStubbings();
stubbings.addAll(fromSingleMock);
}
return stubbings;
}
use of org.mockito.stubbing.Stubbing in project mockito by mockito.
the class UnusedStubbings method reportUnused.
void reportUnused() {
if (unused.isEmpty()) {
return;
}
List<Invocation> invocations = new LinkedList<>();
for (Stubbing stubbing : unused) {
invocations.add(stubbing.getInvocation());
}
if (invocations.isEmpty()) {
return;
}
Reporter.unncessaryStubbingException(invocations);
}
use of org.mockito.stubbing.Stubbing in project powermock by powermock.
the class LocationFromStackTraceTest method should_filter_extra_elements_in_stack_when_mocking_static_method.
@Test
public void should_filter_extra_elements_in_stack_when_mocking_static_method() throws Exception {
mockStatic(SomethingWithStaticMethod.class);
when(SomethingWithStaticMethod.doStaticOne()).thenReturn("Something else 1");
when(SomethingWithStaticMethod.doStaticTwo()).thenReturn("Something else 2");
doNothing().when(SomethingWithStaticMethod.class, "doStaticVoid");
MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getStaticMethodInvocationControl(SomethingWithStaticMethod.class);
MockHandlerAdaptor mockHandlerAdaptor = invocationControl.getMockHandlerAdaptor();
Object mock = mockHandlerAdaptor.getMock();
MockHandler<?> mockHandler = MockUtil.getMockHandler(mock);
InvocationContainerImpl invocationContainer = (InvocationContainerImpl) mockHandler.getInvocationContainer();
List<Stubbing> stubbings = new ArrayList<Stubbing>(invocationContainer.getStubbingsAscending());
assertThat(stubbings.size(), is(3));
Location static1Location = stubbings.get(0).getInvocation().getLocation();
assertThat(static1Location.toString(), is("-> at samples.powermockito.junit4.stacktracecleaner." + "LocationFromStackTraceTest.should_filter_extra_elements_in_stack_when_mocking_static_method(" + "LocationFromStackTraceTest.java:37)"));
Location static2Location = stubbings.get(1).getInvocation().getLocation();
assertThat(static2Location.toString(), is("-> at samples.powermockito.junit4.stacktracecleaner." + "LocationFromStackTraceTest.should_filter_extra_elements_in_stack_when_mocking_static_method(" + "LocationFromStackTraceTest.java:38)"));
Location staticVoidLocation = stubbings.get(2).getInvocation().getLocation();
assertThat(staticVoidLocation.toString(), is("-> at samples.powermockito.junit4.stacktracecleaner." + "LocationFromStackTraceTest.should_filter_extra_elements_in_stack_when_mocking_static_method(" + "LocationFromStackTraceTest.java:39)"));
// Removing these calls and the three Location assertions above will cause the test to fail due to the
// strict stubs. Without the alterations to StackTraceCleanerProvider, the failure messages will contain
// an item in the stack trace inside the powermock libraries.
assertThat(SomethingWithStaticMethod.doStaticOne(), is("Something else 1"));
assertThat(SomethingWithStaticMethod.doStaticTwo(), is("Something else 2"));
SomethingWithStaticMethod.doStaticVoid();
}
Aggregations