Search in sources :

Example 1 with MockSettings

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

the class DebuggerTest method testAddBreakpoint.

@Test
public void testAddBreakpoint() throws Exception {
    MockSettings mockSettings = new MockSettingsImpl<>().defaultAnswer(RETURNS_SMART_NULLS).extraInterfaces(Resource.class);
    Project project = mock(Project.class);
    when(optional.isPresent()).thenReturn(true);
    when(optional.get()).thenReturn(project);
    when(project.getPath()).thenReturn(PATH);
    VirtualFile virtualFile = mock(VirtualFile.class, mockSettings);
    Path path = mock(Path.class);
    when(path.toString()).thenReturn(PATH);
    when(virtualFile.getLocation()).thenReturn(path);
    when(virtualFile.toString()).thenReturn(PATH);
    Resource resource = (Resource) virtualFile;
    when(resource.getRelatedProject()).thenReturn(optional);
    doReturn(promiseVoid).when(service).addBreakpoint(SESSION_ID, breakpointDto);
    doReturn(promiseVoid).when(promiseVoid).then((Operation<Void>) any());
    when(locationDto.withLineNumber(LINE_NUMBER + 1)).thenReturn(locationDto);
    when(locationDto.withResourcePath(PATH)).thenReturn(locationDto);
    when(locationDto.withResourceProjectPath(PATH)).thenReturn(locationDto);
    when(locationDto.withTarget(anyString())).thenReturn(locationDto);
    when(breakpointDto.withLocation(locationDto)).thenReturn(breakpointDto);
    when(breakpointDto.withEnabled(true)).thenReturn(breakpointDto);
    debugger.addBreakpoint(virtualFile, LINE_NUMBER);
    verify(locationDto).withLineNumber(LINE_NUMBER + 1);
    verify(locationDto).withTarget(FQN);
    verify(locationDto).withResourcePath(PATH);
    verify(locationDto).withResourceProjectPath(PATH);
    verify(breakpointDto).withLocation(locationDto);
    verify(breakpointDto).withEnabled(true);
    verify(promiseVoid).then(operationVoidCaptor.capture());
    operationVoidCaptor.getValue().apply(null);
    verify(observer).onBreakpointAdded(breakpointCaptor.capture());
    assertEquals(breakpointCaptor.getValue(), TEST_BREAKPOINT);
    verify(promiseVoid).catchError(operationPromiseErrorCaptor.capture());
    operationPromiseErrorCaptor.getValue().apply(promiseError);
    verify(promiseError).getMessage();
}
Also used : VirtualFile(org.eclipse.che.ide.api.resources.VirtualFile) VariablePath(org.eclipse.che.api.debug.shared.model.VariablePath) Path(org.eclipse.che.ide.resource.Path) Project(org.eclipse.che.ide.api.resources.Project) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Resource(org.eclipse.che.ide.api.resources.Resource) MockSettings(org.mockito.MockSettings) BaseTest(org.eclipse.che.plugin.debugger.ide.BaseTest) Test(org.junit.Test)

Example 2 with MockSettings

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

the class DefaultToBackendFunctionTest method testApply_newField.

@Test
public void testApply_newField() {
    MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
        throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
    });
    BatchMetaDocPart withNewFieldsDocPart = mock(BatchMetaDocPart.class, settings);
    doReturn(false).when(withNewFieldsDocPart).isCreatedOnCurrentBatch();
    doReturn(Lists.newArrayList(new ImmutableMetaField("newFieldName", "newFieldId", FieldType.INTEGER))).when(withNewFieldsDocPart).getOnBatchModifiedMetaFields();
    doReturn(Collections.emptyList()).when(withNewFieldsDocPart).getOnBatchModifiedMetaScalars();
    DocPartData withNewData = mock(DocPartData.class);
    given(withNewData.getMetaDocPart()).willReturn(withNewFieldsDocPart);
    CollectionData collectionData = mock(CollectionData.class);
    given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(withNewData));
    //when
    Iterable<BackendTransactionJob> result = fun.apply(collectionData);
    ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
    //then
    assertEquals("Expected 2 jobs to do, but " + resultList.size() + " were recived", 2, resultList.size());
    {
        Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(withNewData)).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(withNewFieldsDocPart) && castedJob.getField().getName().equals("newFieldName") && castedJob.getField().getIdentifier().equals("newFieldId");
        }).findAny();
        assertTrue(addFieldJob.isPresent());
        int addFieldIndex = resultList.indexOf(addFieldJob.get());
        int insertIndex = resultList.indexOf(insertJob.get());
        assert addFieldIndex >= 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);
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) DocPartData(com.torodb.core.d2r.DocPartData) Optional(java.util.Optional) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) ImmutableMetaField(com.torodb.core.transaction.metainf.ImmutableMetaField) 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)

Example 3 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 4 with MockSettings

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

the class MockitoMockIntegrationTest method whenUsingMockWithSettings_thenCorrect.

@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));
}
Also used : MockSettings(org.mockito.MockSettings) Test(org.junit.Test)

Example 5 with MockSettings

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

the class SpyAnnotationEngine method spyNewInstance.

private static Object spyNewInstance(Object testInstance, Field field) throws InstantiationException, IllegalAccessException, InvocationTargetException {
    MockSettings settings = withSettings().defaultAnswer(CALLS_REAL_METHODS).name(field.getName());
    Class<?> type = field.getType();
    if (type.isInterface()) {
        return Mockito.mock(type, settings.useConstructor());
    }
    int modifiers = type.getModifiers();
    if (typeIsPrivateAbstractInnerClass(type, modifiers)) {
        throw new MockitoException(join("@Spy annotation can't initialize private abstract inner classes.", "  inner class: '" + type.getSimpleName() + "'", "  outer class: '" + type.getEnclosingClass().getSimpleName() + "'", "", "You should augment the visibility of this inner class"));
    }
    if (typeIsNonStaticInnerClass(type, modifiers)) {
        Class<?> enclosing = type.getEnclosingClass();
        if (!enclosing.isInstance(testInstance)) {
            throw new MockitoException(join("@Spy annotation can only initialize inner classes declared in the test.", "  inner class: '" + type.getSimpleName() + "'", "  outer class: '" + enclosing.getSimpleName() + "'", ""));
        }
        return Mockito.mock(type, settings.useConstructor().outerInstance(testInstance));
    }
    Constructor<?> constructor = noArgConstructorOf(type);
    if (Modifier.isPrivate(constructor.getModifiers())) {
        MemberAccessor accessor = Plugins.getMemberAccessor();
        return Mockito.mock(type, settings.spiedInstance(accessor.newInstance(constructor)));
    } else {
        return Mockito.mock(type, settings.useConstructor());
    }
}
Also used : MemberAccessor(org.mockito.plugins.MemberAccessor) MockitoException(org.mockito.exceptions.base.MockitoException) MockSettings(org.mockito.MockSettings)

Aggregations

MockSettings (org.mockito.MockSettings)18 Test (org.junit.Test)10 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 MockName (org.mockito.mock.MockName)2 AddDocPartDdlJob (com.torodb.core.dsl.backend.AddDocPartDdlJob)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Set (java.util.Set)1 SocketFactory (javax.net.SocketFactory)1 VariablePath (org.eclipse.che.api.debug.shared.model.VariablePath)1