Search in sources :

Example 1 with Stubbing

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;
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) LinkedList(java.util.LinkedList)

Example 2 with Stubbing

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();
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) Invocation(org.mockito.invocation.Invocation) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Stubbing

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;
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) StubbingComparator(org.mockito.internal.stubbing.StubbingComparator) TreeSet(java.util.TreeSet) DefaultMockingDetails(org.mockito.internal.util.DefaultMockingDetails)

Example 4 with Stubbing

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);
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) Invocation(org.mockito.invocation.Invocation) LinkedList(java.util.LinkedList)

Example 5 with Stubbing

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();
}
Also used : Stubbing(org.mockito.stubbing.Stubbing) InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) ArrayList(java.util.ArrayList) MockitoMethodInvocationControl(org.powermock.api.mockito.invocation.MockitoMethodInvocationControl) MockHandlerAdaptor(org.powermock.api.mockito.invocation.MockHandlerAdaptor) Location(org.mockito.invocation.Location) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Stubbing (org.mockito.stubbing.Stubbing)8 Invocation (org.mockito.invocation.Invocation)4 LinkedList (java.util.LinkedList)2 TreeSet (java.util.TreeSet)2 StubbingComparator (org.mockito.internal.stubbing.StubbingComparator)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)1 DefaultMockingDetails (org.mockito.internal.util.DefaultMockingDetails)1 ListUtil (org.mockito.internal.util.collections.ListUtil)1 Location (org.mockito.invocation.Location)1 MatchableInvocation (org.mockito.invocation.MatchableInvocation)1 MockHandlerAdaptor (org.powermock.api.mockito.invocation.MockHandlerAdaptor)1 MockitoMethodInvocationControl (org.powermock.api.mockito.invocation.MockitoMethodInvocationControl)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1