Search in sources :

Example 6 with MockSettings

use of org.mockito.MockSettings in project gwt-test-utils by gwt-test-utils.

the class MockitoMockAnnotationProcessor method process.

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());
    }
    // see @Mock answer default value
    mockSettings.defaultAnswer(annotation.answer().get());
    return Mockito.mock(MockAnnotationProcessorHelper.getTypeToMock(field), mockSettings);
}
Also used : MockSettings(org.mockito.MockSettings)

Example 7 with MockSettings

use of org.mockito.MockSettings in project powermock by powermock.

the class AnnotationEnabler method standardInject.

private void standardInject(Object testInstance) throws IllegalAccessException {
    Set<Field> fields = Whitebox.getFieldsAnnotatedWith(testInstance, getMockAnnotations());
    for (Field field : fields) {
        if (field.get(testInstance) != null) {
            continue;
        }
        final Class<?> type = field.getType();
        if (field.isAnnotationPresent(org.powermock.core.classloader.annotations.Mock.class)) {
            org.powermock.core.classloader.annotations.Mock annotation = field.getAnnotation(org.powermock.core.classloader.annotations.Mock.class);
            final String[] value = annotation.value();
            if (value.length != 1 || !"".equals(value[0])) {
                System.err.println("PowerMockito deprecation: Use PowerMockito.spy(..) for partial mocking instead. A standard mock will be created instead.");
            }
        }
        if (field.isAnnotationPresent(org.mockito.Mock.class)) {
            org.mockito.Mock mockAnnotation = field.getAnnotation(org.mockito.Mock.class);
            MockSettings mockSettings = withSettings();
            Answers answers = mockAnnotation.answer();
            if (answers != null) {
                mockSettings.defaultAnswer(answers.get());
            }
            Class<?>[] extraInterfaces = mockAnnotation.extraInterfaces();
            if (extraInterfaces != null && extraInterfaces.length > 0) {
                mockSettings.extraInterfaces(extraInterfaces);
            }
            String name = mockAnnotation.name();
            if (name != null && name.length() > 0) {
                mockSettings.name(name);
            }
            field.set(testInstance, mock(type, mockSettings));
        } else {
            field.set(testInstance, mock(type));
        }
    }
}
Also used : Answers(org.mockito.Answers) Mock(org.mockito.Mock) Field(java.lang.reflect.Field) MockSettings(org.mockito.MockSettings)

Example 8 with MockSettings

use of org.mockito.MockSettings in project mockito by mockito.

the class MockAnnotationProcessor method process.

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());
    return Mockito.mock(field.getType(), mockSettings);
}
Also used : MockSettings(org.mockito.MockSettings)

Example 9 with MockSettings

use of org.mockito.MockSettings in project spring-boot by spring-projects.

the class MockDefinition method createMock.

@SuppressWarnings("unchecked")
public <T> T createMock(String name) {
    MockSettings settings = MockReset.withSettings(getReset());
    if (StringUtils.hasLength(name)) {
        settings.name(name);
    }
    if (!this.extraInterfaces.isEmpty()) {
        settings.extraInterfaces(this.extraInterfaces.toArray(new Class<?>[] {}));
    }
    settings.defaultAnswer(MockitoApi.get().getAnswer(this.answer));
    if (this.serializable) {
        settings.serializable();
    }
    return (T) Mockito.mock(this.typeToMock.resolve(), settings);
}
Also used : MockSettings(org.mockito.MockSettings)

Example 10 with MockSettings

use of org.mockito.MockSettings in project torodb by torodb.

the class DefaultToBackendFunctionTest method testApply_newDocPart.

@Test
public void testApply_newDocPart() {
    MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
        throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
    });
    BatchMetaDocPart allNewDocPart = mock(BatchMetaDocPart.class, settings);
    doReturn(true).when(allNewDocPart).isCreatedOnCurrentBatch();
    doReturn(Lists.newArrayList(Lists.newArrayList(new ImmutableMetaField("newFieldName", "newFieldId", FieldType.BOOLEAN))).stream()).when(allNewDocPart).streamFields();
    doReturn(Lists.newArrayList(new ImmutableMetaScalar("newScalarId", FieldType.BOOLEAN)).stream()).when(allNewDocPart).streamScalars();
    DocPartData allNewData = mock(DocPartData.class);
    given(allNewData.getMetaDocPart()).willReturn(allNewDocPart);
    CollectionData collectionData = mock(CollectionData.class);
    given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(allNewData));
    //when
    Iterable<BackendTransactionJob> result = fun.apply(collectionData);
    ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
    //then
    assertEquals("Expected 4 jobs to do, but " + resultList.size() + " were recived", 4, resultList.size());
    {
        Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(allNewData)).findAny();
        assertTrue(insertJob.isPresent());
        Optional<BackendTransactionJob> addFieldJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddFieldDdlJob)) {
                return false;
            }
            AddFieldDdlJob castedJob = (AddFieldDdlJob) job;
            return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getField().getName().equals("newFieldName") && castedJob.getField().getIdentifier().equals("newFieldId");
        }).findAny();
        assertTrue(addFieldJob.isPresent());
        Optional<BackendTransactionJob> addScalarJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddScalarDddlJob)) {
                return false;
            }
            AddScalarDddlJob castedJob = (AddScalarDddlJob) job;
            return castedJob.getDocPart().equals(allNewDocPart) && castedJob.getScalar().getIdentifier().equals("newScalarId") && castedJob.getScalar().getType().equals(FieldType.BOOLEAN);
        }).findAny();
        assertTrue(addScalarJob.isPresent());
        Optional<BackendTransactionJob> createDocPartJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddDocPartDdlJob)) {
                return false;
            }
            AddDocPartDdlJob castedJob = (AddDocPartDdlJob) job;
            return castedJob.getDocPart().equals(allNewDocPart);
        }).findAny();
        assertTrue(createDocPartJob.isPresent());
        int createDocPartIndex = resultList.indexOf(createDocPartJob.get());
        int addFieldIndex = resultList.indexOf(addFieldJob.get());
        int addScalarIndex = resultList.indexOf(addScalarJob.get());
        int insertIndex = resultList.indexOf(insertJob.get());
        assert createDocPartIndex >= 0;
        assert addFieldIndex >= 0;
        assert addScalarIndex >= 0;
        assert insertIndex >= 0;
        assertTrue("For a given doc part, all related add fields jobs must be executed before insert " + "jobs, but in this case the add field job has index " + addFieldIndex + " and the insert job has index " + insertIndex, addFieldIndex < insertIndex);
        assertTrue("For a given doc part, all related add scalar jobs must be executed before insert " + "jobs, but in this case the add scalr job has index " + addScalarIndex + " and the insert job has index " + insertIndex, addScalarIndex < insertIndex);
        assertTrue("For a given doc part, all related create doc part jobs must be executed " + "before add field jobs, but in this case the create doc part job has index " + createDocPartIndex + " and " + "the add field job has index " + addFieldIndex, createDocPartIndex < addFieldIndex);
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) DocPartData(com.torodb.core.d2r.DocPartData) Optional(java.util.Optional) AddDocPartDdlJob(com.torodb.core.dsl.backend.AddDocPartDdlJob) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) ImmutableMetaScalar(com.torodb.core.transaction.metainf.ImmutableMetaScalar) AddScalarDddlJob(com.torodb.core.dsl.backend.AddScalarDddlJob) InsertBackendJob(com.torodb.core.dsl.backend.InsertBackendJob) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) MockSettings(org.mockito.MockSettings) AddFieldDdlJob(com.torodb.core.dsl.backend.AddFieldDdlJob) Test(org.junit.Test)

Aggregations

MockSettings (org.mockito.MockSettings)12 Test (org.junit.Test)6 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)5 CollectionData (com.torodb.core.d2r.CollectionData)4 DocPartData (com.torodb.core.d2r.DocPartData)4 BackendTransactionJob (com.torodb.core.dsl.backend.BackendTransactionJob)4 InsertBackendJob (com.torodb.core.dsl.backend.InsertBackendJob)4 Optional (java.util.Optional)4 AddFieldDdlJob (com.torodb.core.dsl.backend.AddFieldDdlJob)2 AddScalarDddlJob (com.torodb.core.dsl.backend.AddScalarDddlJob)2 ImmutableMetaField (com.torodb.core.transaction.metainf.ImmutableMetaField)2 ImmutableMetaScalar (com.torodb.core.transaction.metainf.ImmutableMetaScalar)2 AddDocPartDdlJob (com.torodb.core.dsl.backend.AddDocPartDdlJob)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 Set (java.util.Set)1 VariablePath (org.eclipse.che.api.debug.shared.model.VariablePath)1 Project (org.eclipse.che.ide.api.resources.Project)1 Resource (org.eclipse.che.ide.api.resources.Resource)1 VirtualFile (org.eclipse.che.ide.api.resources.VirtualFile)1