當前位置: 首頁>>代碼示例>>Java>>正文


Java Captor類代碼示例

本文整理匯總了Java中org.mockito.Captor的典型用法代碼示例。如果您正苦於以下問題:Java Captor類的具體用法?Java Captor怎麽用?Java Captor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Captor類屬於org.mockito包,在下文中一共展示了Captor類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: reconciledExplicitly

import org.mockito.Captor; //導入依賴的package包/類
/**
 * Verifies that an explicit task reconciliation for the task statuses in the provided persister was invoked.
 */
public static Expect reconciledExplicitly(Persister persisterWithStatuses) {
    // Use a custom comparator for sorting: Protos don't implement Comparable
    final Comparator<Protos.TaskStatus> statusComparator = new Comparator<Protos.TaskStatus>() {
        @Override
        public int compare(TaskStatus o1, TaskStatus o2) {
            return o1.getTaskId().getValue().compareTo(o2.getTaskId().getValue());
        }
    };

    return new Expect() {
        // Use this form instead of using ArgumentCaptor.forClass() to avoid problems with typecasting generics:
        @Captor private ArgumentCaptor<Collection<Protos.TaskStatus>> statusCaptor;

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) {
            MockitoAnnotations.initMocks(this);
            verify(mockDriver, atLeastOnce()).reconcileTasks(statusCaptor.capture());
            Set<Protos.TaskStatus> expected = new TreeSet<>(statusComparator);
            expected.addAll(new StateStore(persisterWithStatuses).fetchStatuses());
            Set<Protos.TaskStatus> got = new TreeSet<>(statusComparator);
            got.addAll(statusCaptor.getValue());
            Assert.assertEquals(expected, got);
        }

        @Override
        public String getDescription() {
            return String.format("Explicit task reconcile call for statuses: %s",
                    new StateStore(persisterWithStatuses).fetchStatuses().stream()
                            .map(status -> String.format("%s=%s", status.getTaskId().getValue(), status.getState()))
                            .collect(Collectors.toList()));
        }
    };
}
 
開發者ID:mesosphere,項目名稱:dcos-commons,代碼行數:37,代碼來源:Expect.java

示例2: injectCaptor

import org.mockito.Captor; //導入依賴的package包/類
private void injectCaptor(Object testInstance) throws Exception {
    Set<Field> fieldsAnnotatedWithCaptor = Whitebox.getFieldsAnnotatedWith(testInstance, Captor.class);
    for (Field field : fieldsAnnotatedWithCaptor) {
        final Object captor = processAnnotationOn(field.getAnnotation(Captor.class),field);
        field.set(testInstance, captor);
    }
}
 
開發者ID:awenblue,項目名稱:powermock,代碼行數:8,代碼來源:AnnotationEnabler.java

示例3: processAnnotationOn

import org.mockito.Captor; //導入依賴的package包/類
private Object processAnnotationOn(Captor annotation, Field field) {
    Class<?> type = field.getType();
    if (!ArgumentCaptor.class.isAssignableFrom(type)) {
        throw new MockitoException("@Captor field must be of the type ArgumentCaptor.\n" + "Field: '"
                + field.getName() + "' has wrong type\n"
                + "For info how to use @Captor annotations see examples in javadoc for MockitoAnnotations class.");
    }
    Class cls = new GenericMaster().getGenericType(field);
    return ArgumentCaptor.forClass(cls);
}
 
開發者ID:awenblue,項目名稱:powermock,代碼行數:11,代碼來源:AnnotationEnabler.java

示例4: test

import org.mockito.Captor; //導入依賴的package包/類
@Test
public void test() {
	UUID dest = system.addActor(new ActorFactory() { 
		@Override
		public Actor create() {
			return new Actor("UnhandledFeatureActor") {
				@Mock
				protected Appender mockAppender;
				@Captor
				protected ArgumentCaptor<LoggingEvent> captorLoggingEvent;
				
				@Override
				public void receive(ActorMessage<?> message) {
					MockitoAnnotations.initMocks(this);
					logger().removeAllAppenders();
					logger().addAppender(mockAppender);
					unhandled(message);
					verify(mockAppender, times(1)).doAppend(captorLoggingEvent.capture());
					LoggingEvent loggingEvent = captorLoggingEvent.getValue();
					assertTrue(loggingEvent.getMessage().toString().contains("Unhandled message"));
				}
			};
		}
	});
	
	system.send(new ActorMessage<Object>(null, 0, system.SYSTEM_ID, dest));
	system.start();
	try {
		Thread.sleep(100);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	system.shutdown(true);
}
 
開發者ID:relvaner,項目名稱:actor4j-core,代碼行數:35,代碼來源:UnhandledFeature.java

示例5: createMockFor

import org.mockito.Captor; //導入依賴的package包/類
@SuppressWarnings("deprecation")
public Object createMockFor(Annotation annotation, Field field) {
    if (annotation instanceof Mock) {
        return processAnnotationOn((Mock) annotation, field);
    }
    if (annotation instanceof MockitoAnnotations.Mock) {
        return processAnnotationOn((MockitoAnnotations.Mock) annotation, field);
    }
    if (annotation instanceof Captor) {
        return processAnnotationOn((Captor) annotation, field);
    }        

    return null;
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:15,代碼來源:DefaultAnnotationEngine.java

示例6: processAnnotationOn

import org.mockito.Captor; //導入依賴的package包/類
private Object processAnnotationOn(Captor annotation, Field field) {
    Class<?> type = field.getType();
    if (!ArgumentCaptor.class.isAssignableFrom(type)) {
        throw new MockitoException("@Captor field must be of the type ArgumentCaptor.\n" + "Field: '"
                + field.getName() + "' has wrong type\n"
                + "For info how to use @Captor annotations see examples in javadoc for MockitoAnnotations class.");
    }
    Class cls = new GenericMaster().getGenericType(field);        
    return ArgumentCaptor.forClass(cls);    
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:11,代碼來源:DefaultAnnotationEngine.java

示例7: scan

import org.mockito.Captor; //導入依賴的package包/類
/**
 * Scan fields annotated by &#064;InjectMocks
 *
 * @return Fields that depends on Mock
 */
private Set<Field> scan() {
    Set<Field> mockDependentFields = new HashSet<Field>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        if (null != field.getAnnotation(InjectMocks.class)) {
            assertNoAnnotations(field, Mock.class, MockitoAnnotations.Mock.class, Captor.class);
            mockDependentFields.add(field);
        }
    }

    return mockDependentFields;
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:18,代碼來源:InjectMocksScanner.java

示例8: process

import org.mockito.Captor; //導入依賴的package包/類
public Object process(Captor annotation, Field field) {
    Class<?> type = field.getType();
    if (!ArgumentCaptor.class.isAssignableFrom(type)) {
        throw new MockitoException("@Captor field must be of the type ArgumentCaptor.\n" + "Field: '"
           + field.getName() + "' has wrong type\n"
           + "For info how to use @Captor annotations see examples in javadoc for MockitoAnnotations class.");
    }
    Class cls = new GenericMaster().getGenericType(field);
    return ArgumentCaptor.forClass(cls);
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:11,代碼來源:CaptorAnnotationProcessor.java

示例9: launchedTasks

import org.mockito.Captor; //導入依賴的package包/類
/**
 * Verifies that a pod was launched with exactly the provided task names.
 */
public static Expect launchedTasks(Collection<String> taskNames) {
    return new Expect() {
        // Use this form instead of using ArgumentCaptor.forClass() to avoid problems with typecasting generics:
        @Captor private ArgumentCaptor<Collection<Protos.OfferID>> offerIdsCaptor;
        @Captor private ArgumentCaptor<Collection<Protos.Offer.Operation>> operationsCaptor;

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) {
            MockitoAnnotations.initMocks(this);
            verify(mockDriver, atLeastOnce())
                    .acceptOffers(offerIdsCaptor.capture(), operationsCaptor.capture(), any());
            Protos.OfferID lastAcceptedOfferId = offerIdsCaptor.getValue().iterator().next();
            Assert.assertEquals(String.format(
                        "Expected last offer with ID %s to be accepted, but last accepted offer was %s",
                        state.getLastOffer().getId().getValue(), lastAcceptedOfferId.getValue()),
                    state.getLastOffer().getId(), lastAcceptedOfferId);
            Collection<String> launchedTaskNames = new ArrayList<>();
            // A single acceptOffers() call may contain multiple LAUNCH/LAUNCH_GROUP operations.
            // We want to ensure they're all counted as a unit when tallying the pod.
            Collection<Protos.TaskInfo> launchedTaskInfos = new ArrayList<>();
            for (Protos.Offer.Operation operation : operationsCaptor.getValue()) {
                if (operation.getType().equals(Protos.Offer.Operation.Type.LAUNCH)) {
                    // Old-style launch with custom executor
                    launchedTaskNames.addAll(operation.getLaunch().getTaskInfosList().stream()
                            .map(task -> task.getName())
                            .collect(Collectors.toList()));
                    launchedTaskInfos.addAll(operation.getLaunch().getTaskInfosList().stream()
                            .map(task -> TaskPackingUtils.unpack(task))
                            .collect(Collectors.toList()));
                } else if (operation.getType().equals(Protos.Offer.Operation.Type.LAUNCH_GROUP)) {
                    // New-style launch with default executor
                    launchedTaskNames.addAll(operation.getLaunch().getTaskInfosList().stream()
                            .map(task -> task.getName())
                            .collect(Collectors.toList()));
                    launchedTaskInfos.addAll(operation.getLaunchGroup().getTaskGroup().getTasksList());
                }
            }
            if (!launchedTaskInfos.isEmpty()) {
                state.addLaunchedPod(launchedTaskInfos);
            }
            Assert.assertTrue(
                    String.format("Expected launched tasks: %s, got tasks: %s", taskNames, launchedTaskNames),
                    launchedTaskNames.containsAll(taskNames) && taskNames.containsAll(launchedTaskNames));
        }

        @Override
        public String getDescription() {
            return String.format("Tasks were launched into a pod: %s", taskNames);
        }
    };
}
 
開發者ID:mesosphere,項目名稱:dcos-commons,代碼行數:55,代碼來源:Expect.java

示例10: unreservedTasks

import org.mockito.Captor; //導入依賴的package包/類
/**
 * Verifies that the resources for the provided task names have been unreserved.
 */
public static Expect unreservedTasks(Collection<String> taskNames) {
    return new Expect() {
        // Use this form instead of using ArgumentCaptor.forClass() to avoid problems with typecasting generics:
        @Captor private ArgumentCaptor<Collection<Protos.OfferID>> offerIdsCaptor;
        @Captor private ArgumentCaptor<Collection<Protos.Offer.Operation>> operationsCaptor;

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) {
            MockitoAnnotations.initMocks(this);
            verify(mockDriver, atLeastOnce())
                    .acceptOffers(offerIdsCaptor.capture(), operationsCaptor.capture(), any());
            Assert.assertEquals(state.getLastOffer().getId(), offerIdsCaptor.getValue().iterator().next());
            Collection<String> expectedResourceIds = taskNames.stream()
                    .map(taskName ->
                            ResourceUtils.getResourceIds(state.getLastLaunchedTask(taskName).getResourcesList()))
                    .flatMap(List::stream)
                    .collect(Collectors.toList());
            Assert.assertFalse(String.format("Expected some resource ids for tasks: %s, got none", taskNames),
                    expectedResourceIds.isEmpty());
            Collection<String> unreservedResourceIds = new ArrayList<>();
            for (Protos.Offer.Operation operation : operationsCaptor.getValue()) {
                if (operation.getType().equals(Protos.Offer.Operation.Type.DESTROY)) {
                    // Destroy volume(s)
                    unreservedResourceIds.addAll(
                            ResourceUtils.getResourceIds(operation.getDestroy().getVolumesList()));
                } else if (operation.getType().equals(Protos.Offer.Operation.Type.UNRESERVE)) {
                    // Unreserve resource(s)
                    unreservedResourceIds.addAll(
                            ResourceUtils.getResourceIds(operation.getUnreserve().getResourcesList()));
                }
            }
            Assert.assertTrue(
                    String.format("Expected unreserved resource ids: %s, got ids: %s",
                            expectedResourceIds, unreservedResourceIds),
                    unreservedResourceIds.containsAll(expectedResourceIds)
                    && expectedResourceIds.containsAll(unreservedResourceIds));
        }

        @Override
        public String getDescription() {
            return String.format("Resources for tasks have been unreserved: %s", taskNames);
        }
    };
}
 
開發者ID:mesosphere,項目名稱:dcos-commons,代碼行數:48,代碼來源:Expect.java

示例11: initializeCaptors

import org.mockito.Captor; //導入依賴的package包/類
private void initializeCaptors(Object instance) {
    List<InstanceField> instanceFields = Fields.allDeclaredFieldsOf(instance).filter(annotatedBy(Captor.class)).instanceFields();
    for (InstanceField instanceField : instanceFields) {
        instanceField.set(new CaptorAnnotationProcessor().process(instanceField.annotation(Captor.class), instanceField.jdkField()));
    }
}
 
開發者ID:marcingrzejszczak,項目名稱:mockito-cookbook,代碼行數:7,代碼來源:MockitoBeforeTestNGMethod.java

示例12: shouldNotAllowCaptorAndMock

import org.mockito.Captor; //導入依賴的package包/類
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Mock @Captor ArgumentCaptor captor;
    });
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:7,代碼來源:WrongSetOfAnnotationsTest.java

示例13: shouldNotAllowCaptorAndSpy

import org.mockito.Captor; //導入依賴的package包/類
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Spy @Captor ArgumentCaptor captor;
    });
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:7,代碼來源:WrongSetOfAnnotationsTest.java

示例14: shouldNotAllowCaptorAndInjectMock

import org.mockito.Captor; //導入依賴的package包/類
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Captor ArgumentCaptor captor;
    });
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:7,代碼來源:WrongSetOfAnnotationsTest.java

示例15: DefaultAnnotationEngine

import org.mockito.Captor; //導入依賴的package包/類
public DefaultAnnotationEngine() {
    registerAnnotationProcessor(Mock.class, new MockAnnotationProcessor());
    registerAnnotationProcessor(MockitoAnnotations.Mock.class, new MockitoAnnotationsMockAnnotationProcessor());
    registerAnnotationProcessor(Captor.class, new CaptorAnnotationProcessor());
}
 
開發者ID:SpoonLabs,項目名稱:astor,代碼行數:6,代碼來源:DefaultAnnotationEngine.java


注:本文中的org.mockito.Captor類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。