org.easymock.EasyMock.mock()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(151)

本文整理了Java中org.easymock.EasyMock.mock()方法的一些代码示例,展示了EasyMock.mock()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EasyMock.mock()方法的具体详情如下:
包路径:org.easymock.EasyMock
类名称:EasyMock
方法名:mock

EasyMock.mock介绍

[英]Creates a mock object that implements the given interface, order checking is disabled by default.
[中]创建实现给定接口的模拟对象,默认情况下禁用顺序检查。

代码示例

代码示例来源:origin: org.easymock/easymock

  1. /**
  2. * Creates a mock object that implements the given interface, order checking
  3. * is disabled by default.
  4. * <p>
  5. * <b>Note:</b> This is the old version of {@link #mock(Class)}, which is more completion friendly
  6. *
  7. * @param toMock
  8. * the class or interface that should be mocked.
  9. * @param <T>
  10. * the interface that the mock object should implement. It is expected to be of
  11. * class {@code toMock}.
  12. * @return the mock object.
  13. */
  14. public static <T> T createMock(Class<?> toMock) {
  15. return mock(toMock);
  16. }

代码示例来源:origin: org.easymock/easymock

  1. /**
  2. * Creates a mock object that implements the given interface, order checking
  3. * is disabled by default.
  4. * <p>
  5. * <b>Note:</b> This is the old version of {@link #mock(String, Class)}, which is more completion friendly
  6. *
  7. * @param name
  8. * the name of the mock object.
  9. * @param toMock
  10. * the class or interface that should be mocked.
  11. * @param <T>
  12. * the interface that the mock object should implement. It is expected to be of
  13. * class {@code toMock}.
  14. * @return the mock object.
  15. * @throws IllegalArgumentException
  16. * if the name is not a valid Java identifier.
  17. */
  18. public static <T> T createMock(String name, Class<?> toMock) {
  19. return mock(name, toMock);
  20. }

代码示例来源:origin: org.easymock/easymock

  1. /**
  2. * Creates a mock object, of the requested type, that implements the given interface
  3. * or extends the given class.
  4. * <p>
  5. * <b>Note:</b> This is the old version of {@link #mock(MockType, Class)}, which is more completion friendly
  6. *
  7. * @param type
  8. * the type of the mock to be created.
  9. * @param toMock
  10. * the class or interface that should be mocked.
  11. * @param <T>
  12. * the interface that the mock object should implement. It is expected to be of
  13. * class {@code toMock}.
  14. * @return the mock object.
  15. * @since 3.2
  16. */
  17. public static <T> T createMock(MockType type, Class<?> toMock) {
  18. return mock(type, toMock);
  19. }

代码示例来源:origin: org.easymock/easymock

  1. /**
  2. * Creates a mock object, of the requested type and name, that implements the given interface
  3. * or extends the given class
  4. * <p>
  5. * <b>Note:</b> This is the old version of {@link #mock(String, MockType, Class)}, which is more completion friendly
  6. *
  7. * @param name
  8. * the name of the mock object.
  9. * @param type
  10. * the type of the mock to be created.
  11. * @param toMock
  12. * the class or interface that should be mocked.
  13. * @param <T>
  14. * the interface that the mock object should implement. It is expected to be of
  15. * class {@code toMock}.
  16. * @return the mock object.
  17. * @since 3.2
  18. */
  19. public static <T> T createMock(String name, MockType type, Class<?> toMock) {
  20. return mock(name, type, toMock);
  21. }

代码示例来源:origin: confluentinc/ksql

  1. private UriInfo uriInfo(final String uri) throws URISyntaxException {
  2. final UriInfo uriInfo = mock(UriInfo.class);
  3. expect(uriInfo.getAbsolutePath()).andReturn(new URI(uri));
  4. replay(uriInfo);
  5. return uriInfo;
  6. }
  7. }

代码示例来源:origin: confluentinc/ksql

  1. @Before
  2. public void setUp() {
  3. schemaRegistryClient = mock(SchemaRegistryClient.class);
  4. }

代码示例来源:origin: apache/incubator-druid

  1. private StorageLocation fakeLocation(long total, long free, long max, Double percent)
  2. {
  3. File file = EasyMock.mock(File.class);
  4. EasyMock.expect(file.getTotalSpace()).andReturn(total).anyTimes();
  5. EasyMock.expect(file.getFreeSpace()).andReturn(free).anyTimes();
  6. EasyMock.replay(file);
  7. return new StorageLocation(file, max, percent);
  8. }

代码示例来源:origin: confluentinc/ksql

  1. @Test
  2. public void shouldEnsureRewriteRequirementCorrectly() {
  3. assertThat("Query should be valid for rewrite for struct.", StatementRewriteForStruct.requiresRewrite(EasyMock.mock(Query.class)));
  4. assertThat("CSAS should be valid for rewrite for struct.", StatementRewriteForStruct.requiresRewrite(EasyMock.mock(CreateStreamAsSelect.class)));
  5. assertThat("CTAS should be valid for rewrite for struct.", StatementRewriteForStruct.requiresRewrite(EasyMock.mock(CreateTableAsSelect.class)));
  6. assertThat("Insert Into should be valid for rewrite for struct.", StatementRewriteForStruct.requiresRewrite(EasyMock.mock(InsertInto.class)));
  7. }

代码示例来源:origin: confluentinc/ksql

  1. private StatusResource getTestStatusResource() {
  2. final StatementExecutor mockStatementExecutor = mock(StatementExecutor.class);
  3. expect(mockStatementExecutor.getStatuses()).andReturn(mockCommandStatuses);
  4. for (final Map.Entry<CommandId, CommandStatus> commandEntry : mockCommandStatuses.entrySet()) {
  5. expect(mockStatementExecutor.getStatus(commandEntry.getKey())).andReturn(Optional.of(commandEntry.getValue()));
  6. }
  7. expect(mockStatementExecutor.getStatus(anyObject(CommandId.class))).andReturn(Optional.empty());
  8. replay(mockStatementExecutor);
  9. return new StatusResource(mockStatementExecutor);
  10. }

代码示例来源:origin: confluentinc/ksql

  1. @Test
  2. public void shouldFailTestIfStatementShouldBeRewritten() {
  3. assertThat("Incorrect rewrite requirement enforcement.", !StatementRewriteForStruct.requiresRewrite(EasyMock.mock(CreateTable.class)));
  4. }

代码示例来源:origin: apache/incubator-druid

  1. @Test
  2. public void cleanup()
  3. {
  4. PooledTopNAlgorithm pooledTopNAlgorithm = new PooledTopNAlgorithm(EasyMock.mock(StorageAdapter.class), null, null);
  5. PooledTopNAlgorithm.PooledTopNParams params = EasyMock.createMock(PooledTopNAlgorithm.PooledTopNParams.class);
  6. ResourceHolder<ByteBuffer> resourceHolder = EasyMock.createMock(ResourceHolder.class);
  7. EasyMock.expect(params.getResultsBufHolder()).andReturn(resourceHolder).times(1);
  8. EasyMock.expect(resourceHolder.get()).andReturn(ByteBuffer.allocate(1)).times(1);
  9. resourceHolder.close();
  10. EasyMock.expectLastCall().once();
  11. EasyMock.replay(params);
  12. EasyMock.replay(resourceHolder);
  13. pooledTopNAlgorithm.cleanup(params);
  14. EasyMock.verify(params);
  15. EasyMock.verify(resourceHolder);
  16. }
  17. }

代码示例来源:origin: linkedin/cruise-control

  1. private HttpServletRequest prepareRequest(HttpSession session, String userTaskId, String resource, Map<String, String []> params) {
  2. HttpServletRequest request = EasyMock.mock(HttpServletRequest.class);
  3. EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
  4. EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
  5. EasyMock.expect(request.getMethod()).andReturn("GET").anyTimes();
  6. EasyMock.expect(request.getRequestURI()).andReturn(resource).anyTimes();
  7. EasyMock.expect(request.getParameterMap()).andReturn(params).anyTimes();
  8. EasyMock.expect(request.getHeader(UserTaskManager.USER_TASK_HEADER_NAME)).andReturn(userTaskId).anyTimes();
  9. EasyMock.expect(request.getRemoteHost()).andReturn("test-host").anyTimes();
  10. for (String headerName : KafkaCruiseControlServletUtils.HEADERS_TO_TRY) {
  11. EasyMock.expect(request.getHeader(headerName)).andReturn("localhost").anyTimes();
  12. }
  13. EasyMock.replay(request);
  14. return request;
  15. }
  16. }

代码示例来源:origin: confluentinc/ksql

  1. @Test
  2. public void testHandle() throws IOException {
  3. final HttpResponse response = mock(HttpResponse.class);
  4. final StatusLine statusLine = mock(StatusLine.class);
  5. final HttpEntity entity = mock(HttpEntity.class);
  6. final Logger log = mock(Logger.class);
  7. final Header header = mock(Header.class);
  8. expect(response.getStatusLine()).andReturn(statusLine).once();
  9. expect(statusLine.getStatusCode()).andReturn(HttpStatus.SC_OK).once();
  10. expect(response.getEntity()).andReturn(entity).times(2);
  11. final ByteArrayInputStream bais = new ByteArrayInputStream("yolo".getBytes(StandardCharsets.UTF_8));
  12. expect(entity.getContent()).andReturn(bais).times(2);
  13. expect(entity.getContentType()).andReturn(header).times(1);
  14. expect(header.getElements()).andReturn(new HeaderElement[]{});
  15. expect(entity.getContentLength()).andReturn(4L).times(2);
  16. log.warn("yolo");
  17. expectLastCall().once();
  18. replay(response, statusLine, entity, header, log);
  19. final KsqlVersionCheckerResponseHandler kvcr = new KsqlVersionCheckerResponseHandler(log);
  20. kvcr.handle(response);
  21. verify(response, statusLine, entity, header, log);
  22. }
  23. }

代码示例来源:origin: apache/incubator-druid

  1. @Test
  2. public void testCleanupWithNullParams()
  3. {
  4. PooledTopNAlgorithm pooledTopNAlgorithm = new PooledTopNAlgorithm(EasyMock.mock(StorageAdapter.class), null, null);
  5. pooledTopNAlgorithm.cleanup(null);
  6. }

代码示例来源:origin: confluentinc/ksql

  1. @Before
  2. public void setup() {
  3. expect(mockKsqlEngine.isAcceptingStatements()).andReturn(true);
  4. expect(serviceContext.getTopicClient()).andReturn(mockKafkaTopicClient);
  5. expect(mockKsqlEngine.hasActiveQueries()).andReturn(false);
  6. statement = new PreparedStatement<>("s", mock(Statement.class));
  7. expect(mockStatementParser.parseSingleStatement(queryString))
  8. .andReturn(statement);
  9. replay(mockKsqlEngine, mockStatementParser);
  10. testResource = new StreamedQueryResource(
  11. ksqlConfig,
  12. mockKsqlEngine,
  13. serviceContext,
  14. mockStatementParser,
  15. commandQueue,
  16. DISCONNECT_CHECK_INTERVAL,
  17. activenessRegistrar);
  18. }

代码示例来源:origin: linkedin/cruise-control

  1. private BrokerFailureDetector createBrokerFailureDetector(Queue<Anomaly> anomalies, Time time) {
  2. LoadMonitor mockLoadMonitor = EasyMock.mock(LoadMonitor.class);
  3. KafkaCruiseControl mockKafkaCruiseControl = EasyMock.mock(KafkaCruiseControl.class);
  4. EasyMock.expect(mockLoadMonitor.brokersWithPartitions(anyLong())).andAnswer(() -> new HashSet<>(Arrays.asList(0, 1))).anyTimes();
  5. EasyMock.replay(mockLoadMonitor);
  6. Properties props = KafkaCruiseControlUnitTestUtils.getKafkaCruiseControlProperties();
  7. props.setProperty(KafkaCruiseControlConfig.ZOOKEEPER_CONNECT_CONFIG, zookeeper().getConnectionString());
  8. KafkaCruiseControlConfig kafkaCruiseControlConfig = new KafkaCruiseControlConfig(props);
  9. return new BrokerFailureDetector(kafkaCruiseControlConfig,
  10. mockLoadMonitor,
  11. anomalies,
  12. time,
  13. mockKafkaCruiseControl);
  14. }

代码示例来源:origin: confluentinc/ksql

  1. @Test
  2. @SuppressWarnings("unchecked")
  3. public void shouldUseAThreadLocalSerializer() throws InterruptedException {
  4. final List<Serializer<GenericRow>> serializers = new LinkedList<>();
  5. final ThreadLocalSerializer serializer = new ThreadLocalSerializer(
  6. () -> {
  7. final Serializer<GenericRow> local = mock(Serializer.class);
  8. serializers.add(local);
  9. expect(local.serialize(anyString(), anyObject(GenericRow.class)))
  10. .andReturn(new byte[32])
  11. .times(1);
  12. replay(local);
  13. return serializers.get(serializers.size() - 1);
  14. }
  15. );
  16. for (int i = 0; i < 3; i++) {
  17. final Thread t = new Thread(
  18. () -> serializer.serialize("foo", new GenericRow(Collections.emptyList()))
  19. );
  20. t.start();
  21. t.join();
  22. assertThat(serializers.size(), equalTo(i + 1));
  23. serializers.forEach(EasyMock::verify);
  24. }
  25. }
  26. }

代码示例来源:origin: confluentinc/ksql

  1. @Test
  2. public void shouldPrintErrorIfCantConnectToRestServer() throws Exception {
  3. givenRunInteractivelyWillExit();
  4. final KsqlRestClient mockRestClient = EasyMock.mock(KsqlRestClient.class);
  5. EasyMock.expect(mockRestClient.makeRootRequest()).andThrow(new KsqlRestClientException("Boom", new ProcessingException("")));
  6. EasyMock.expect(mockRestClient.getServerInfo()).andReturn(
  7. RestResponse.of(new ServerInfo("1.x", "testClusterId", "testServiceId")));
  8. EasyMock.expect(mockRestClient.getServerAddress()).andReturn(new URI("http://someserver:8008")).anyTimes();
  9. EasyMock.replay(mockRestClient);
  10. new Cli(1L, 1L, mockRestClient, console)
  11. .runInteractively();
  12. assertThat(terminal.getOutputString(), containsString("Remote server address may not be valid"));
  13. }

代码示例来源:origin: confluentinc/ksql

  1. @Test
  2. public void shouldPrintErrorOnUnsupportedAPI() throws Exception {
  3. givenRunInteractivelyWillExit();
  4. final KsqlRestClient mockRestClient = EasyMock.mock(KsqlRestClient.class);
  5. EasyMock.expect(mockRestClient.makeRootRequest()).andReturn(
  6. RestResponse.erroneous(
  7. new KsqlErrorMessage(
  8. Errors.toErrorCode(NOT_ACCEPTABLE.getStatusCode()),
  9. "Minimum supported client version: 1.0")));
  10. EasyMock.expect(mockRestClient.getServerInfo()).andReturn(
  11. RestResponse.of(new ServerInfo("1.x", "testClusterId", "testServiceId")));
  12. EasyMock.expect(mockRestClient.getServerAddress()).andReturn(new URI("http://someserver:8008"));
  13. EasyMock.replay(mockRestClient);
  14. new Cli(1L, 1L, mockRestClient, console)
  15. .runInteractively();
  16. Assert.assertThat(
  17. terminal.getOutputString(),
  18. containsString("This CLI version no longer supported"));
  19. Assert.assertThat(
  20. terminal.getOutputString(),
  21. containsString("Minimum supported client version: 1.0"));
  22. }

代码示例来源:origin: googleapis/google-cloud-java

  1. @Test
  2. public void testBatch() {
  3. RpcBatch batchMock = EasyMock.mock(RpcBatch.class);
  4. EasyMock.expect(storageRpcMock.createBatch()).andReturn(batchMock);
  5. EasyMock.replay(batchMock, storageRpcMock);
  6. initializeService();
  7. StorageBatch batch = storage.batch();
  8. assertSame(options, batch.getOptions());
  9. assertSame(storageRpcMock, batch.getStorageRpc());
  10. assertSame(batchMock, batch.getBatch());
  11. EasyMock.verify(batchMock);
  12. }

相关文章