Using Capture in Easy Mock to Write Test for Exception
Best Java code snippets using org.easymock.EasyMock.capture (Showing top 20 results out of 387)
Refine search
@ Test public void testReset() { Capture<String> id1 = Capture. newInstance (); Capture<String> id2 = Capture. newInstance (); EasyMock. expect (taskMaster.getSupervisorManager()). andReturn (Optional.of(supervisorManager)).times( 2 ); EasyMock. expect (supervisorManager.resetSupervisor(EasyMock. capture (id1), EasyMock.anyObject(DataSourceMetadata. class ))). andReturn ( true ); EasyMock. expect (supervisorManager.resetSupervisor(EasyMock. capture (id2), EasyMock.anyObject(DataSourceMetadata. class ))). andReturn ( false ); replayAll(); Response response = supervisorResource.reset( "my-id" ); Assert. assertEquals ( 200 , response.getStatus()); Assert. assertEquals (ImmutableMap.of( "id" , "my-id" ), response.getEntity()); response = supervisorResource.reset( "my-id-2" ); Assert. assertEquals ( 404 , response.getStatus()); Assert. assertEquals ( "my-id" , id1. getValue ()); Assert. assertEquals ( "my-id-2" , id2. getValue ()); verifyAll(); resetAll(); EasyMock. expect (taskMaster.getSupervisorManager()). andReturn (Optional.absent()); replayAll(); response = supervisorResource.terminate( "my-id" ); Assert. assertEquals ( 503 , response.getStatus()); verifyAll(); }
@ Test public void testStopSendingAfterClose() { replayOnSubscribe(); EasyMock. expect (session.getAsyncRemote()). andReturn (async).anyTimes(); final Capture<String> json = EasyMock.newCapture(CaptureType.ALL); async.sendText(EasyMock. capture (json), EasyMock.anyObject()); subscription.request( 1 ); subscription.cancel(); EasyMock. replay (subscription, session, async); subscriber.onNext(ImmutableList.of(ImmutableMap.of( "a" , 1 ))); subscriber.close(); subscriber.onNext(ImmutableList.of(ImmutableMap.of( "b" , 2 ), ImmutableMap.of( "c" , 3 ))); assertEquals (ImmutableList.of( "{\"a\":1}" ), json.getValues()); EasyMock.verify(subscription, session, async); }
@ Test public void testOnError() throws Exception { replayOnSubscribe(); final Capture<CloseReason> reason = EasyMock.newCapture(); EasyMock. expect (session.getId()). andReturn ( "abc123" ).once(); session.close(EasyMock. capture (reason)); EasyMock.expectLastCall().once(); subscription.cancel(); EasyMock.expectLastCall().once(); EasyMock. replay (subscription, session); subscriber.onError( new RuntimeException( "streams died" )); subscriber.close(); assertEquals ( "streams exception" , reason. getValue ().getReasonPhrase()); assertEquals (CloseCodes.UNEXPECTED_CONDITION, reason. getValue ().getCloseCode()); EasyMock.verify(subscription, session); } }
@ Test public void testGetZoneNotFound() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<ManagedZone>> callback = Capture. newInstance (); Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture. newInstance (); batchMock.addGetZone( EasyMock.eq(ZONE_NAME), EasyMock. capture (callback), EasyMock. capture (capturedOptions)); EasyMock. replay (batchMock); DnsBatchResult<Zone> batchResult = dnsBatch.getZone(ZONE_NAME); assertEquals ( 0 , capturedOptions. getValue ().size()); GoogleJsonError error = new GoogleJsonError(); error.setCode( 404 ); RpcBatch.Callback<ManagedZone> capturedCallback = callback. getValue (); capturedCallback.onFailure(error); assertNull(batchResult.get()); }
@SuppressWarnings( "unchecked" ) @ Test public void shouldCreateSessionWindowedStreamWithInactiviyGap() { EasyMock. expect (groupedStreamMock.windowedBy(EasyMock. capture (sessionWindows))). andReturn (sessionWindowed); EasyMock. expect (sessionWindowed.aggregate(same(initializer), same(aggregator), anyObject(Merger. class ), same(materialized))). andReturn (null); EasyMock. replay (groupedStreamMock, aggregator, sessionWindowed); expression.applyAggregate(groupedStreamMock, initializer, aggregator, materialized); assertThat(sessionWindows. getValue ().inactivityGap(), equalTo(5000L)); EasyMock.verify(groupedStreamMock); }
@ Test public void testGetProjectWithOptions() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<Project>> callback = Capture. newInstance (); Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture. newInstance (); batchMock.addGetProject(EasyMock. capture (callback), EasyMock. capture (capturedOptions)); EasyMock. replay (batchMock); DnsBatchResult<ProjectInfo> batchResult = dnsBatch.getProject(PROJECT_FIELDS); assertNotNull(callback. getValue ()); String selector = (String) capturedOptions. getValue ().get(PROJECT_FIELDS.getRpcOption()); assertTrue(selector.contains(Dns.ProjectField.QUOTA.getSelector())); assertTrue(selector.contains(Dns.ProjectField.PROJECT_ID.getSelector())); RpcBatch.Callback<Project> capturedCallback = callback. getValue (); capturedCallback.onSuccess(PROJECT_INFO.toPb()); assertEquals (PROJECT_INFO, batchResult.get()); }
@ Test public void testSanity() throws Exception { replayOnSubscribe(); EasyMock. expect (session.getAsyncRemote()). andReturn (async).anyTimes(); final Capture<String> json = EasyMock.newCapture(CaptureType.ALL); async.sendText(EasyMock. capture (json), EasyMock.anyObject()); EasyMock.expectLastCall().times( 3 ); subscription.request( 1 ); EasyMock.expectLastCall().once(); session.close(EasyMock.anyObject()); EasyMock.expectLastCall().once(); subscription.cancel(); EasyMock. replay (subscription, session, async); subscriber.onNext(ImmutableList.of(ImmutableMap.of( "a" , 1 ), ImmutableMap.of( "b" , 2 ), ImmutableMap.of( "c" , 3 ))); assertEquals (ImmutableList.of( "{\"a\":1}" , "{\"b\":2}" , "{\"c\":3}" ), json.getValues()); subscriber.onComplete(); subscriber.close(); EasyMock.verify(subscription, session, async); }
@ Test public void testProjectGetWithOptions() { Capture<Map<DnsRpc.Option, Object>> capturedOptions = Capture. newInstance (); EasyMock. expect (dnsRpcMock.getProject(EasyMock. capture (capturedOptions))) . andReturn (PROJECT_INFO.toPb()); EasyMock. replay (dnsRpcMock); dns = options.getService(); ProjectInfo projectInfo = dns.getProject(PROJECT_FIELDS); String selector = (String) capturedOptions. getValue ().get(PROJECT_FIELDS.getRpcOption()); assertEquals (PROJECT_INFO, projectInfo); assertTrue(selector.contains(Dns.ProjectField.QUOTA.getSelector())); assertTrue(selector.contains(Dns.ProjectField.PROJECT_ID.getSelector())); }
@ Test public void testDeleteZoneOnSuccess() { EasyMock.reset(batchMock); Capture<RpcBatch.Callback<Void>> callback = Capture. newInstance (); batchMock.addDeleteZone(EasyMock.eq(ZONE_NAME), EasyMock. capture (callback)); EasyMock. replay (batchMock); DnsBatchResult<Boolean> batchResult = dnsBatch.deleteZone(ZONE_NAME); assertNotNull(callback. getValue ()); RpcBatch.Callback<Void> capturedCallback = callback. getValue (); Void result = null; capturedCallback.onSuccess(result); assertTrue(batchResult.get()); }
@SuppressWarnings( "unchecked" ) @ Test public void shouldGetMergerForSessionWindowsFromUdafAggregator() { EasyMock. expect (groupedStreamMock.windowedBy(EasyMock. capture (sessionWindows))). andReturn (sessionWindowed); EasyMock. expect (sessionWindowed.aggregate(same(initializer), same(aggregator), same(merger), same(materialized))). andReturn (null); EasyMock. expect (aggregator.getMerger()). andReturn (merger); EasyMock. replay (groupedStreamMock, aggregator, sessionWindowed); expression.applyAggregate(groupedStreamMock, initializer, aggregator, materialized); EasyMock.verify(aggregator); }
@ Test public void testConstructor() { WebSecurityManager securityManager = createMock(WebSecurityManager. class ); FilterChainResolver filterChainResolver = createMock(FilterChainResolver. class ); ServletContext servletContext = createMock(ServletContext. class ); Capture<WebGuiceEnvironment> capture = new Capture<WebGuiceEnvironment>(); servletContext.setAttribute(eq(EnvironmentLoaderListener.ENVIRONMENT_ATTRIBUTE_KEY), and(anyObject(WebGuiceEnvironment. class ), capture (capture))); replay (servletContext, securityManager, filterChainResolver); WebGuiceEnvironment underTest = new WebGuiceEnvironment(filterChainResolver, servletContext, securityManager); assertSame(securityManager, underTest.getSecurityManager()); assertSame(filterChainResolver, underTest.getFilterChainResolver()); assertSame(securityManager, underTest.getWebSecurityManager()); assertSame(servletContext, underTest.getServletContext()); assertSame(underTest, capture. getValue ()); verify(servletContext); } }
@ Test public void shouldExitAndDrainIfQueryStopsRunning() throws Exception { expect (queryMetadata.isRunning()). andReturn ( true ). andReturn ( false ); expect (rowQueue.drainTo( capture (drainCapture))).andAnswer(rows( "Row1" , "Row2" , "Row3" )); createWriter(); writer.write(out); final List<String> lines = getOutput(out); assertThat(lines, hasItems( containsString( "Row1" ), containsString( "Row2" ), containsString( "Row3" ))); }
@ Test public void testCreateImageWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture. newInstance (); EasyMock. expect (computeRpcMock.createImage(eq(IMAGE.toPb()), capture (capturedOptions))) . andReturn (globalOperation.toPb()); EasyMock. replay (computeRpcMock); compute = options.getService(); Operation operation = compute.create(IMAGE, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions. getValue ().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains( "selfLink" )); assertTrue(selector.contains( "id" )); assertTrue(selector.contains( "description" )); assertEquals ( 23 , selector.length()); assertEquals (globalOperation, operation); }
@ Test public void testCreateNetworkWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture. newInstance (); EasyMock. expect (computeRpcMock.createNetwork(eq(NETWORK.toPb()), capture (capturedOptions))) . andReturn (globalOperation.toPb()); EasyMock. replay (computeRpcMock); compute = options.getService(); Operation operation = compute.create(NETWORK, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions. getValue ().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains( "selfLink" )); assertTrue(selector.contains( "id" )); assertTrue(selector.contains( "description" )); assertEquals ( 23 , selector.length()); assertEquals (globalOperation, operation); }
@ Test public void testFailedNew() throws Exception EasyMock. expect (request.getContentType()). andReturn (MediaType.APPLICATION_JSON).once(); EasyMock. expect (request.getRemoteAddr()). andReturn (ip).once(); final Capture<AuditInfo> auditInfoCapture = Capture. newInstance (); final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager. class ); EasyMock. expect (lookupCoordinatorManager.updateLookups( EasyMock.eq(SINGLE_TIER_MAP), EasyMock. capture (auditInfoCapture) )). andReturn ( false ).once(); EasyMock. replay (lookupCoordinatorManager, request); ); Assert. assertEquals ( 500 , response.getStatus()); Assert. assertEquals (ImmutableMap.of( "error" , "Unknown error updating configuration" ), response.getEntity()); Assert.assertTrue(auditInfoCapture.hasCaptured()); final AuditInfo auditInfo = auditInfoCapture. getValue (); Assert. assertEquals (author, auditInfo.getAuthor()); Assert. assertEquals (comment, auditInfo.getComment()); Assert. assertEquals (ip, auditInfo.getIp());
@ Test public void testGetRegionWithSelectedFields() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture. newInstance (); EasyMock. expect (computeRpcMock.getRegion(eq(REGION_ID.getRegion()), capture (capturedOptions))) . andReturn (REGION.toPb()); EasyMock. replay (computeRpcMock); compute = options.getService(); Region region = compute.getRegion(REGION_ID.getRegion(), REGION_OPTION_FIELDS); String selector = (String) capturedOptions. getValue ().get(REGION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains( "selfLink" )); assertTrue(selector.contains( "id" )); assertTrue(selector.contains( "description" )); assertEquals ( 23 , selector.length()); assertEquals (REGION, region); }
@ Test public void testGetZoneWithSelectedFields() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture. newInstance (); EasyMock. expect (computeRpcMock.getZone(eq(ZONE_ID.getZone()), capture (capturedOptions))) . andReturn (ZONE.toPb()); EasyMock. replay (computeRpcMock); compute = options.getService(); Zone zone = compute.getZone(ZONE_ID.getZone(), ZONE_OPTION_FIELDS); String selector = (String) capturedOptions. getValue ().get(ZONE_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains( "selfLink" )); assertTrue(selector.contains( "id" )); assertTrue(selector.contains( "description" )); assertEquals ( 23 , selector.length()); assertEquals (ZONE, zone); }
@ Test public void testExceptionalNew() throws Exception EasyMock. expect (request.getContentType()). andReturn (MediaType.APPLICATION_JSON).once(); EasyMock. expect (request.getRemoteAddr()). andReturn (ip).once(); final Capture<AuditInfo> auditInfoCapture = Capture. newInstance (); final LookupCoordinatorManager lookupCoordinatorManager = EasyMock.createStrictMock( LookupCoordinatorManager. class ); EasyMock. expect (lookupCoordinatorManager.updateLookups( EasyMock.eq(SINGLE_TIER_MAP), EasyMock. capture (auditInfoCapture) )).andThrow( new RuntimeException(errMsg)).once(); EasyMock. replay (lookupCoordinatorManager, request); ); Assert. assertEquals ( 500 , response.getStatus()); Assert. assertEquals (ImmutableMap.of( "error" , errMsg), response.getEntity()); Assert.assertTrue(auditInfoCapture.hasCaptured()); final AuditInfo auditInfo = auditInfoCapture. getValue (); Assert. assertEquals (author, auditInfo.getAuthor()); Assert. assertEquals (comment, auditInfo.getComment()); Assert. assertEquals (ip, auditInfo.getIp());
@ Test public void testDeleteSnapshotWithSelectedFields_Operation() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture. newInstance (); EasyMock. expect ( computeRpcMock.deleteSnapshot(eq(SNAPSHOT_ID.getSnapshot()), capture (capturedOptions))) . andReturn (globalOperation.toPb()); EasyMock. replay (computeRpcMock); compute = options.getService(); Operation operation = compute.deleteSnapshot(SNAPSHOT_ID, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions. getValue ().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains( "selfLink" )); assertTrue(selector.contains( "id" )); assertTrue(selector.contains( "description" )); assertEquals ( 23 , selector.length()); assertEquals (globalOperation, operation); }
@ Test public void testCreateGlobalAddressWithOptions() { Capture<Map<ComputeRpc.Option, Object>> capturedOptions = Capture. newInstance (); EasyMock. expect ( computeRpcMock.createGlobalAddress(eq(GLOBAL_ADDRESS.toPb()), capture (capturedOptions))) . andReturn (globalOperation.toPb()); EasyMock. replay (computeRpcMock); compute = options.getService(); Operation operation = compute.create(GLOBAL_ADDRESS, OPERATION_OPTION_FIELDS); String selector = (String) capturedOptions. getValue ().get(OPERATION_OPTION_FIELDS.getRpcOption()); assertTrue(selector.contains( "selfLink" )); assertTrue(selector.contains( "id" )); assertTrue(selector.contains( "description" )); assertEquals ( 23 , selector.length()); assertEquals (globalOperation, operation); }
Source: https://www.tabnine.com/code/java/methods/org.easymock.EasyMock/capture
0 Response to "Using Capture in Easy Mock to Write Test for Exception"
Post a Comment