use of org.mockito.invocation.Invocation in project mockito by mockito.
the class WarningsCollector method getWarnings.
public String getWarnings() {
List<Invocation> unused = new UnusedStubsFinder().find(createdMocks);
List<Invocation> all = AllInvocationsFinder.find(createdMocks);
List<InvocationMatcher> allInvocationMatchers = InvocationMatcher.createFrom(all);
return new WarningsPrinterImpl(unused, allInvocationMatchers, false).print();
}
use of org.mockito.invocation.Invocation in project mockito by mockito.
the class Reporter method potentialStubbingProblem.
public static void potentialStubbingProblem(Invocation actualInvocation, Collection<Invocation> argMismatchStubbings) {
StringBuilder stubbings = new StringBuilder();
int count = 1;
for (Invocation s : argMismatchStubbings) {
stubbings.append(" ").append(count++).append(". ").append(s);
stubbings.append("\n ").append(s.getLocation()).append("\n");
}
//remove trailing end of line
stubbings.deleteCharAt(stubbings.length() - 1);
throw new PotentialStubbingProblem(join("Strict stubbing argument mismatch. Please check:", " - this invocation of '" + actualInvocation.getMethod().getName() + "' method:", " " + actualInvocation, " " + actualInvocation.getLocation(), " - has following stubbing(s) with different arguments:", stubbings, "Typically, stubbing argument mismatch indicates user mistake when writing tests.", "Mockito fails early so that you can debug potential problem easily.", "However, there are legit scenarios when this exception generates false negative signal:", " - stubbing the same method multiple times using 'given().will()' or 'when().then()' API", " Please use 'will().given()' or 'doReturn().when()' API for stubbing.", " - stubbed method is intentionally invoked with different arguments by code under test", " Please use 'default' or 'silent' JUnit Rule.", "For more information see javadoc for PotentialStubbingProblem class."));
}
use of org.mockito.invocation.Invocation in project mockito by mockito.
the class MockitoCore method getLastInvocation.
/**
* For testing purposes only. Is not the part of main API.
*
* @return last invocation
*/
public Invocation getLastInvocation() {
OngoingStubbingImpl ongoingStubbing = ((OngoingStubbingImpl) mockingProgress().pullOngoingStubbing());
List<Invocation> allInvocations = ongoingStubbing.getRegisteredInvocations();
return allInvocations.get(allInvocations.size() - 1);
}
use of org.mockito.invocation.Invocation in project mockito by mockito.
the class MockHandlerFactoryTest method valid_handle_result_is_permitted.
@Test
public //see issue 331
void valid_handle_result_is_permitted() throws Throwable {
//given:
MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(123));
InternalMockHandler<?> handler = createMockHandler(settings);
mock.intReturningMethod();
Invocation invocation = super.getLastInvocation();
//when:
Object result = handler.handle(invocation);
//then
assertEquals(123, result);
}
use of org.mockito.invocation.Invocation in project mockito by mockito.
the class MockHandlerFactoryTest method handle_result_must_not_be_null_for_primitives.
@Test
public //see issue 331
void handle_result_must_not_be_null_for_primitives() throws Throwable {
//given:
MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(null));
InternalMockHandler<?> handler = createMockHandler(settings);
mock.intReturningMethod();
Invocation invocation = super.getLastInvocation();
//when:
Object result = handler.handle(invocation);
//then null value is not a valid result for a primitive
assertNotNull(result);
assertEquals(0, result);
}
Aggregations