Search in sources :

Example 11 with MockSettingsImpl

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

the class DefaultToBackendFunctionTest method testApply_noChanges.

@Test
public void testApply_noChanges() {
    MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
        throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
    });
    BatchMetaDocPart allCreatedDocPart = mock(BatchMetaDocPart.class, settings);
    doReturn(false).when(allCreatedDocPart).isCreatedOnCurrentBatch();
    doReturn(Collections.emptyList()).when(allCreatedDocPart).getOnBatchModifiedMetaFields();
    doReturn(Collections.emptyList()).when(allCreatedDocPart).getOnBatchModifiedMetaScalars();
    DocPartData allCreatedData = mock(DocPartData.class);
    given(allCreatedData.getMetaDocPart()).willReturn(allCreatedDocPart);
    CollectionData collectionData = mock(CollectionData.class);
    given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(allCreatedData));
    //when
    Iterable<BackendTransactionJob> result = fun.apply(collectionData);
    ArrayList<BackendTransactionJob> resultList = Lists.newArrayList(result);
    //then
    assertEquals("Expected 1 jobs to do, but " + resultList.size() + " were recived", 1, resultList.size());
    {
        Optional<BackendTransactionJob> insertJob = resultList.stream().filter((job) -> job instanceof InsertBackendJob && ((InsertBackendJob) job).getDataToInsert().equals(allCreatedData)).findAny();
        assertTrue(insertJob.isPresent());
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) InsertBackendJob(com.torodb.core.dsl.backend.InsertBackendJob) DocPartData(com.torodb.core.d2r.DocPartData) Optional(java.util.Optional) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) BackendTransactionJob(com.torodb.core.dsl.backend.BackendTransactionJob) MockSettings(org.mockito.MockSettings) Test(org.junit.Test)

Example 12 with MockSettingsImpl

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

the class DefaultToBackendFunctionTest method testApply_newScalar.

@Test
public void testApply_newScalar() {
    MockSettings settings = new MockSettingsImpl().defaultAnswer((t) -> {
        throw new AssertionError("Method " + t.getMethod() + " was not expected to be called");
    });
    BatchMetaDocPart withNewScalarDocPart = mock(BatchMetaDocPart.class, settings);
    doReturn(false).when(withNewScalarDocPart).isCreatedOnCurrentBatch();
    doReturn(Collections.emptyList()).when(withNewScalarDocPart).getOnBatchModifiedMetaFields();
    doReturn(Lists.newArrayList(new ImmutableMetaScalar("newScalarId", FieldType.INTEGER))).when(withNewScalarDocPart).getOnBatchModifiedMetaScalars();
    DocPartData withNewScalar = mock(DocPartData.class);
    given(withNewScalar.getMetaDocPart()).willReturn(withNewScalarDocPart);
    CollectionData collectionData = mock(CollectionData.class);
    given(collectionData.orderedDocPartData()).willReturn(Lists.<DocPartData>newArrayList(withNewScalar));
    //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(withNewScalar)).findAny();
        assertTrue(insertJob.isPresent());
        Optional<BackendTransactionJob> addScalarJob = resultList.stream().filter((job) -> {
            if (!(job instanceof AddScalarDddlJob)) {
                return false;
            }
            AddScalarDddlJob castedJob = (AddScalarDddlJob) job;
            return castedJob.getDocPart().equals(withNewScalarDocPart) && castedJob.getScalar().getIdentifier().equals("newScalarId") && castedJob.getScalar().getType().equals(FieldType.INTEGER);
        }).findAny();
        assertTrue(addScalarJob.isPresent());
        int addScalarIndex = resultList.indexOf(addScalarJob.get());
        int insertIndex = resultList.indexOf(insertJob.get());
        assert addScalarIndex >= 0;
        assert insertIndex >= 0;
        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);
    }
}
Also used : CollectionData(com.torodb.core.d2r.CollectionData) DocPartData(com.torodb.core.d2r.DocPartData) Optional(java.util.Optional) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) 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) Test(org.junit.Test)

Example 13 with MockSettingsImpl

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

the class MockitoCore method mock.

public <T> T mock(Class<T> typeToMock, MockSettings settings) {
    if (!MockSettingsImpl.class.isInstance(settings)) {
        throw new IllegalArgumentException("Unexpected implementation of '" + settings.getClass().getCanonicalName() + "'\n" + "At the moment, you cannot provide your own implementations of that class.");
    }
    MockSettingsImpl impl = MockSettingsImpl.class.cast(settings);
    MockCreationSettings<T> creationSettings = impl.confirm(typeToMock);
    T mock = createMock(creationSettings);
    mockingProgress().mockingStarted(mock, creationSettings);
    return mock;
}
Also used : MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl)

Example 14 with MockSettingsImpl

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

the class InlineByteBuddyMockMakerTest method settingsFor.

private static <T> MockCreationSettings<T> settingsFor(Class<T> type, Class<?>... extraInterfaces) {
    MockSettingsImpl<T> mockSettings = new MockSettingsImpl<T>();
    mockSettings.setTypeToMock(type);
    mockSettings.defaultAnswer(new Returns("bar"));
    if (extraInterfaces.length > 0)
        mockSettings.extraInterfaces(extraInterfaces);
    return mockSettings;
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl)

Example 15 with MockSettingsImpl

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

the class MockHandlerImplTest method should_remove_verification_mode_even_when_invalid_matchers.

@Test
public void should_remove_verification_mode_even_when_invalid_matchers() throws Throwable {
    // given
    Invocation invocation = new InvocationBuilder().toInvocation();
    @SuppressWarnings("rawtypes") MockHandlerImpl<?> handler = new MockHandlerImpl(new MockSettingsImpl());
    mockingProgress().verificationStarted(VerificationModeFactory.atLeastOnce());
    handler.matchersBinder = new MatchersBinder() {

        public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) {
            throw new InvalidUseOfMatchersException();
        }
    };
    try {
        // when
        handler.handle(invocation);
        // then
        fail();
    } catch (InvalidUseOfMatchersException ignored) {
    }
    assertNull(mockingProgress().pullVerificationMode());
}
Also used : InvalidUseOfMatchersException(org.mockito.exceptions.misusing.InvalidUseOfMatchersException) Invocation(org.mockito.invocation.Invocation) MatchersBinder(org.mockito.internal.invocation.MatchersBinder) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) StubbedInvocationMatcher(org.mockito.internal.stubbing.StubbedInvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) ArgumentMatcherStorage(org.mockito.internal.progress.ArgumentMatcherStorage) 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