下面列出了怎么用org.mockito.MockSettings的API类实例代码及写法,或者点击链接到github查看源代码。
public Object process(Mock annotation, Field field) {
MockSettings mockSettings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) { // never null
mockSettings.extraInterfaces(annotation.extraInterfaces());
}
if ("".equals(annotation.name())) {
mockSettings.name(field.getName());
} else {
mockSettings.name(annotation.name());
}
if(annotation.serializable()){
mockSettings.serializable();
}
// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer().get());
return Mockito.mock(field.getType(), mockSettings);
}
@Override
public Object getParameterValue(Parameter parameter) {
Mock annotation = parameter.getAnnotation(Mock.class);
MockSettings settings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) {
settings.extraInterfaces(annotation.extraInterfaces());
}
if (annotation.serializable()) {
settings.serializable();
}
settings.name(annotation.name().isEmpty() ? parameter.getName() : annotation.name());
settings.defaultAnswer(annotation.answer());
return Mockito.mock(parameter.getType(), settings);
}
private Object processAnnotationOn(Mock annotation, Field field) {
MockSettings mockSettings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) { // never null
mockSettings.extraInterfaces(annotation.extraInterfaces());
}
if ("".equals(annotation.name())) {
mockSettings.name(field.getName());
} else {
mockSettings.name(annotation.name());
}
// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer().get());
return Mockito.mock(field.getType(), mockSettings);
}
private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata) {
MockSettings mockSettings = returnTypeGenericMetadata.hasRawExtraInterfaces() ?
withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces())
: withSettings();
return mockSettings
.serializable()
.defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata));
}
public MockSettings invocationListeners(InvocationListener... listeners) {
if (listeners == null || listeners.length == 0) {
new Reporter().invocationListenersRequiresAtLeastOneListener();
}
for (InvocationListener listener : listeners) {
if (listener == null) {
new Reporter().invocationListenerDoesNotAcceptNullParameters();
}
this.invocationListeners.add(listener);
}
return this;
}
@Test
public void whenUsingMockWithSettings_thenCorrect() {
MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
MyList listMock = mock(MyList.class, customSettings);
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
@Test
public void whenUsingMockWithSettings_thenCorrect() {
MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
MyList listMock = mock(MyList.class, customSettings);
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
/**
* Delegates call to {@link Mockito#mock(Class, MockSettings)}.
*/
default <T> T mock(Class<T> classToMock, MockSettings mockSettings) {
return Mockito.mock(classToMock, mockSettings);
}
/**
* Delegates call to {@link Mockito#withSettings()}.
*/
default MockSettings withSettings() {
return Mockito.withSettings();
}
public void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings) {
if (listener != null && listener instanceof MockingStartedListener) {
((MockingStartedListener) listener).mockingStarted(mock, classToMock, mockSettings);
}
validateMostStuff();
}
public void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings) {
threadSafely().mockingStarted(mock, classToMock, mockSettings);
}
public MockSettings serializable() {
this.serializable = true;
return this;
}
public void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings) {
toBeFilled.add(mock);
}
public MockSettings serializable() {
return serializable(SerializableMode.BASIC);
}
public MockSettings serializable(SerializableMode mode) {
this.serializableMode = mode;
return this;
}
public MockSettings verboseLogging() {
if (!invocationListenersContainsType(VerboseMockInvocationLogger.class)) {
invocationListeners(new VerboseMockInvocationLogger());
}
return this;
}
/**
* Sets up mocking for all static methods of a class with custom {@link MockSettings}.
* <p>This changes the behavior of <u>all</u> static methods calls for <u>all</u>
* invocations. In most cases using {@link #spyStatic(Class)} and stubbing only a few
* methods can be used.
*
* @param clazz The class to set up static mocking for
* @param settings Settings used to set up the mock.
* @return This builder
*/
@UnstableApi
public <T> StaticMockitoSessionBuilder mockStatic(Class<T> clazz, MockSettings settings) {
staticMockings.add(new StaticMocking<>(clazz, () -> Mockito.mock(clazz, settings)));
return this;
}
void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings);
void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings);