org.mockito.MockSettings Java Examples

The following examples show how to use org.mockito.MockSettings. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MockAnnotationProcessor.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #2
Source File: MockitoMockUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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));
}
 
Example #3
Source File: MockitoMockIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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));
}
 
Example #4
Source File: MockSettingsImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: DefaultAnnotationEngine.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #6
Source File: MockParameterFactory.java    From junit5-extensions with MIT License 5 votes vote down vote up
@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);
}
 
Example #7
Source File: ReturnsDeepStubs.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata) {
    MockSettings mockSettings = returnTypeGenericMetadata.hasRawExtraInterfaces() ?
            withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces())
            : withSettings();

    return mockSettings
      .serializable()
            .defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata));
}
 
Example #8
Source File: CollectCreatedMocks.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings) {
    toBeFilled.add(mock);
}
 
Example #9
Source File: MockSettingsImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockSettings verboseLogging() {
    if (!invocationListenersContainsType(VerboseMockInvocationLogger.class)) {
        invocationListeners(new VerboseMockInvocationLogger());
    }
    return this;
}
 
Example #10
Source File: MockSettingsImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockSettings serializable(SerializableMode mode) {
    this.serializableMode = mode;
    return this;
}
 
Example #11
Source File: MockSettingsImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockSettings serializable() {
    return serializable(SerializableMode.BASIC);
}
 
Example #12
Source File: MockSettingsImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockSettings serializable() {
    this.serializable = true;
    return this;
}
 
Example #13
Source File: ThreadSafeMockingProgress.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings) {
    threadSafely().mockingStarted(mock, classToMock, mockSettings);
}
 
Example #14
Source File: MockingProgressImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings) {
    if (listener != null && listener instanceof MockingStartedListener) {
        ((MockingStartedListener) listener).mockingStarted(mock, classToMock, mockSettings);
    }
    validateMostStuff();
}
 
Example #15
Source File: WithMockito.java    From mockito-java8 with Apache License 2.0 4 votes vote down vote up
/**
 * Delegates call to {@link Mockito#withSettings()}.
 */
default MockSettings withSettings() {
    return Mockito.withSettings();
}
 
Example #16
Source File: WithMockito.java    From mockito-java8 with Apache License 2.0 4 votes vote down vote up
/**
 * Delegates call to {@link Mockito#mock(Class, MockSettings)}.
 */
default <T> T mock(Class<T> classToMock, MockSettings mockSettings) {
    return Mockito.mock(classToMock, mockSettings);
}
 
Example #17
Source File: StaticMockitoSessionBuilder.java    From dexmaker with Apache License 2.0 2 votes vote down vote up
/**
 * 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;
}
 
Example #18
Source File: MockingStartedListener.java    From astor with GNU General Public License v2.0 votes vote down vote up
void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings); 
Example #19
Source File: MockingProgress.java    From astor with GNU General Public License v2.0 votes vote down vote up
void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings);