Search in sources :

Example 6 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl in project mockito by mockito.

the class MockHandlerFactoryTest method handle_result_must_not_be_null_for_primitives.

@Test
public //see issue 331
void handle_result_must_not_be_null_for_primitives() throws Throwable {
    //given:
    MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(null));
    InternalMockHandler<?> handler = createMockHandler(settings);
    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();
    //when:
    Object result = handler.handle(invocation);
    //then null value is not a valid result for a primitive
    assertNotNull(result);
    assertEquals(0, result);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockCreationSettings(org.mockito.mock.MockCreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Test(org.junit.Test)

Example 7 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl in project mockito by mockito.

the class MockHandlerImplTest method should_report_bogus_default_answer.

@Test(expected = WrongTypeOfReturnValue.class)
public void should_report_bogus_default_answer() throws Throwable {
    MockSettingsImpl mockSettings = mock(MockSettingsImpl.class);
    MockHandlerImpl<?> handler = new MockHandlerImpl(mockSettings);
    given(mockSettings.getDefaultAnswer()).willReturn(new Returns(AWrongType.WRONG_TYPE));
    // otherwise cast is not done
    @SuppressWarnings("unused") String there_should_not_be_a_CCE_here = (String) handler.handle(new InvocationBuilder().method(Object.class.getDeclaredMethod("toString")).toInvocation());
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 8 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl in project mockito by mockito.

the class InvocationContainerImplStubbingTest method setup.

@Before
public void setup() {
    state = mockingProgress();
    invocationContainerImpl = new InvocationContainerImpl(new MockSettingsImpl());
    invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationBuilder().toInvocationMatcher());
    invocationContainerImplStubOnly = new InvocationContainerImpl(new MockSettingsImpl().stubOnly());
    invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationBuilder().toInvocationMatcher());
    simpleMethod = new InvocationBuilder().simpleMethod().toInvocation();
}
Also used : MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Before(org.junit.Before)

Example 9 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl 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 10 with MockSettingsImpl

use of org.mockito.internal.creation.MockSettingsImpl 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

MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)19 Test (org.junit.Test)14 MockSettings (org.mockito.MockSettings)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 List (java.util.List)4 Optional (java.util.Optional)4 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)4 Returns (org.mockito.internal.stubbing.answers.Returns)4 MockName (org.mockito.mock.MockName)4 Invocation (org.mockito.invocation.Invocation)3 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 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)2 MockCreationSettings (org.mockito.mock.MockCreationSettings)2 AddDocPartDdlJob (com.torodb.core.dsl.backend.AddDocPartDdlJob)1