下面列出了怎么用org.mockito.internal.debugging.LocationImpl的API类实例代码及写法,或者点击链接到github查看源代码。
public void incorrectUseOfAdditionalMatchers(String additionalMatcherName, int expectedSubMatchersCount, Collection<LocalizedMatcher> matcherStack) {
throw new InvalidUseOfMatchersException(join(
"Invalid use of argument matchers inside additional matcher " + additionalMatcherName + " !",
new LocationImpl(),
"",
expectedSubMatchersCount + " sub matchers expected, " + matcherStack.size() + " recorded:",
locationsOf(matcherStack),
"",
"This exception may occur if matchers are combined with raw values:",
" //incorrect:",
" someMethod(AdditionalMatchers.and(isNotNull(), \"raw String\");",
"When using matchers, all arguments have to be provided by matchers.",
"For example:",
" //correct:",
" someMethod(AdditionalMatchers.and(isNotNull(), eq(\"raw String\"));",
"",
"For more info see javadoc for Matchers and AdditionalMatchers classes.",
""
));
}
public void argumentsAreDifferent(String wanted, String actual, Location actualLocation) {
String message = join("Argument(s) are different! Wanted:",
wanted,
new LocationImpl(),
"Actual invocation has different arguments:",
actual,
actualLocation,
""
);
if (JUnitTool.hasJUnit()) {
throw JUnitTool.createArgumentsAreDifferentException(message, wanted, actual);
} else {
throw new ArgumentsAreDifferent(message);
}
}
@Test
public void shouldBeSafeInCaseForSomeReasonFilteredStackTraceIsEmpty() {
//given
StackTraceFilter filterReturningEmptyArray = new StackTraceFilter() {
@Override
public StackTraceElement[] filter(StackTraceElement[] target, boolean keepTop) {
return new StackTraceElement[0];
}
};
//when
String loc = new LocationImpl(filterReturningEmptyArray).toString();
//then
assertEquals("-> at <<unknown line>>", loc);
}
public void incorrectUseOfApi() {
throw new MockitoException(join(
"Incorrect use of API detected here:",
new LocationImpl(),
"",
"You probably stored a reference to OngoingStubbing returned by when() and called stubbing methods like thenReturn() on this reference more than once.",
"Examples of correct usage:",
" when(mock.isOk()).thenReturn(true).thenReturn(false).thenThrow(exception);",
" when(mock.isOk()).thenReturn(true, false).thenThrow(exception);",
""
));
}
public void reportNoSubMatchersFound(String additionalMatcherName) {
throw new InvalidUseOfMatchersException(join(
"No matchers found for additional matcher " + additionalMatcherName,
new LocationImpl(),
""
));
}
private String createWantedButNotInvokedMessage(DescribedInvocation wanted) {
return join(
"Wanted but not invoked:",
wanted.toString(),
new LocationImpl(),
""
);
}
public void wantedButNotInvokedInOrder(DescribedInvocation wanted, DescribedInvocation previous) {
throw new VerificationInOrderFailure(join(
"Verification in order failure",
"Wanted but not invoked:",
wanted.toString(),
new LocationImpl(),
"Wanted anywhere AFTER following interaction:",
previous.toString(),
previous.getLocation(),
""
));
}
private String createTooManyInvocationsMessage(int wantedCount, int actualCount, DescribedInvocation wanted,
Location firstUndesired) {
return join(
wanted.toString(),
"Wanted " + pluralize(wantedCount) + ":",
new LocationImpl(),
"But was " + pluralize(actualCount) + ". Undesired invocation:",
firstUndesired,
""
);
}
public void neverWantedButInvoked(DescribedInvocation wanted, Location firstUndesired) {
throw new NeverWantedButInvoked(join(
wanted.toString(),
"Never wanted here:",
new LocationImpl(),
"But invoked here:",
firstUndesired,
""
));
}
private String createTooLittleInvocationsMessage(org.mockito.internal.reporting.Discrepancy discrepancy, DescribedInvocation wanted,
Location lastActualInvocation) {
String ending =
(lastActualInvocation != null)? lastActualInvocation + "\n" : "\n";
String message = join(
wanted.toString(),
"Wanted " + discrepancy.getPluralizedWantedCount() + ":",
new LocationImpl(),
"But was " + discrepancy.getPluralizedActualCount() + ":",
ending
);
return message;
}
public void noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations) {
ScenarioPrinter scenarioPrinter = new ScenarioPrinter();
String scenario = scenarioPrinter.print(invocations);
throw new NoInteractionsWanted(join(
"No interactions wanted here:",
new LocationImpl(),
"But found this interaction:",
undesired.getLocation(),
scenario
));
}
public void noMoreInteractionsWantedInOrder(Invocation undesired) {
throw new VerificationInOrderFailure(join(
"No interactions wanted here:",
new LocationImpl(),
"But found this interaction:",
undesired.getLocation(),
""
));
}
public void smartNullPointerException(String invocation, Location location) {
throw new SmartNullPointerException(join(
"You have a NullPointerException here:",
new LocationImpl(),
"because this method call was *not* stubbed correctly:",
location,
invocation,
""
));
}
public InvocationImpl(Object mock, MockitoMethod mockitoMethod, Object[] args, int sequenceNumber, RealMethod realMethod) {
this.method = mockitoMethod;
this.mock = mock;
this.realMethod = realMethod;
this.arguments = ArgumentsProcessor.expandVarArgs(mockitoMethod.isVarArgs(), args);
this.rawArguments = args;
this.sequenceNumber = sequenceNumber;
this.location = new LocationImpl();
}
public Object answer(final InvocationOnMock invocation) throws Throwable {
Object defaultReturnValue = delegate.answer(invocation);
if (defaultReturnValue != null) {
return defaultReturnValue;
}
Class<?> type = invocation.getMethod().getReturnType();
if (!type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) {
final Location location = new LocationImpl();
return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, location));
}
return null;
}
public LocalizedMatcher(Matcher actualMatcher) {
this.actualMatcher = actualMatcher;
this.location = new LocationImpl();
}
public void stubbingStarted() {
validateState();
stubbingInProgress = new LocationImpl();
}
@Test
public void shouldLocationNotContainGetStackTraceMethod() {
assertContains("shouldLocationNotContainGetStackTraceMethod", new LocationImpl().toString());
}
public LocalizedMatcher(Matcher actualMatcher) {
this.actualMatcher = actualMatcher;
this.location = new LocationImpl();
}