@Test public void shouldCallMigratorForEveryVersionFromTheProvidedOneToTheLatest() throws Exception { handler.responseMessageForParseDirectory("{ \"target_version\": \"0\", \"something\": \"value\" }"); verify(configRepoMigrator).migrate(anyString(), eq(1)); verify(configRepoMigrator).migrate(nullable(String.class), eq(2)); verify(configRepoMigrator, times(JsonMessageHandler1_0.CURRENT_CONTRACT_VERSION)).migrate(nullable(String.class), anyInt()); }
@Test public void shouldExecuteTheTask() { ActionWithReturn actionWithReturn = mock(ActionWithReturn.class); when(actionWithReturn.execute(any(JsonBasedPluggableTask.class), nullable(GoPluginDescriptor.class))).thenReturn(ExecutionResult.success("yay")); ExecutionResult executionResult = extension.execute(pluginId, actionWithReturn); verify(actionWithReturn).execute(any(JsonBasedPluggableTask.class), nullable(GoPluginDescriptor.class)); assertThat(executionResult.getMessagesForDisplay(), is("yay")); assertTrue(executionResult.isSuccessful()); }
@Test public void savePropertiesShouldFlushAndCloseWriterEvenOnException() { final File filePath = getPathTo(""); try { propertyTools.saveProperties(filePath.getAbsolutePath(), new Properties(), null); fail("Should fail to save properties"); } catch (final IOException e) { verify(ioTools).flush(nullable(FileWriter.class)); verify(ioTools).close(nullable(FileWriter.class)); } }
} else { GenericClass gc = new GenericClass(type); return Mockito.nullable(gc.getRawClass());
private void initCampaign(){ mockCampaign = Mockito.mock(Campaign.class); CampaignOptions mockCampaignOptions = Mockito.mock(CampaignOptions.class); Mockito.when(mockCampaignOptions.usePeacetimeCost()).thenReturn(true); Mockito.when(mockCampaignOptions.getUnitRatingMethod()).thenReturn(mekhq.campaign.rating.UnitRatingMethod.CAMPAIGN_OPS); JumpPath mockJumpPath = Mockito.mock(JumpPath.class); Mockito.when(mockJumpPath.getJumps()).thenReturn(2); Mockito.when(mockCampaign.calculateJumpPath(Mockito.nullable(Planet.class), Mockito.nullable(Planet.class))).thenReturn(mockJumpPath); Mockito.when(mockCampaign.calculateCostPerJump(Mockito.anyBoolean(), Mockito.anyBoolean())).thenReturn((long) 5); Mockito.when(mockCampaign.getUnitRatingMod()).thenReturn(10); Mockito.when(mockCampaign.getOverheadExpenses()).thenReturn((long) 1); Mockito.when(mockCampaign.getContractBase()).thenReturn((long) 10); Mockito.when(mockCampaign.getCampaignOptions()).thenReturn(mockCampaignOptions); Mockito.when(mockCampaign.getPeacetimeCost()).thenReturn(1L); Mockito.when(mockCampaign.getCalendar()).thenReturn(new GregorianCalendar(3067, Calendar.JANUARY, 1)); } }
@Test public void noPreviousContextOnLogin() throws Exception { KerberosConnection krbUtil = mock(KerberosConnection.class); Subject subject = new Subject(); Subject loggedInSubject = new Subject(); Configuration conf = mock(Configuration.class); LoginContext context = mock(LoginContext.class); // Call the real login(LoginContext, Configuration, Subject) method when(krbUtil.login(nullable(LoginContext.class), any(Configuration.class), any(Subject.class))) .thenCallRealMethod(); // Return a fake LoginContext when(krbUtil.createLoginContext(conf)).thenReturn(context); // Return a fake Subject from that fake LoginContext when(context.getSubject()).thenReturn(loggedInSubject); Entry<LoginContext, Subject> pair = krbUtil.login(null, conf, subject); // Verify we get the fake LoginContext and Subject assertEquals(context, pair.getKey()); assertEquals(loggedInSubject, pair.getValue()); // login should be called on the LoginContext verify(context).login(); }
@Test public void noPreviousContextOnLogin() throws Exception { KerberosConnection krbUtil = mock(KerberosConnection.class); Subject subject = new Subject(); Subject loggedInSubject = new Subject(); Configuration conf = mock(Configuration.class); LoginContext context = mock(LoginContext.class); // Call the real login(LoginContext, Configuration, Subject) method when(krbUtil.login(nullable(LoginContext.class), any(Configuration.class), any(Subject.class))) .thenCallRealMethod(); // Return a fake LoginContext when(krbUtil.createLoginContext(conf)).thenReturn(context); // Return a fake Subject from that fake LoginContext when(context.getSubject()).thenReturn(loggedInSubject); Entry<LoginContext, Subject> pair = krbUtil.login(null, conf, subject); // Verify we get the fake LoginContext and Subject assertEquals(context, pair.getKey()); assertEquals(loggedInSubject, pair.getValue()); // login should be called on the LoginContext verify(context).login(); }
@Test public void currentDateToStringShouldUseDateToStringWithNull() { when(dateTools.dateToString(nullable(Date.class), anyString())).thenReturn("the date"); final String dateAsString = dateTools.currentDateToString("EEEE, d MMMM yyyy"); assertEquals("the date", dateAsString); verify(dateTools).dateToString(null, "EEEE, d MMMM yyyy"); }
@Test public void loadPropertiesShouldCloseInputStreamEvenOnException() { try { propertyTools.loadProperties("nothing"); fail("Should fail to load properties"); } catch (final IOException e) { verify(ioTools).close(nullable(InputStream.class)); } }
@Test public void testFailedResponseSerialization() throws IOException { @SuppressWarnings("unchecked") final AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class); final Request request = Mockito.mock(Request.class); final Response response = Mockito.mock(Response.class); final IOException exception = new IOException(); final ErrorResponse errorResponse = Mockito.mock(ErrorResponse.class); final String serializedErrorResponse = "An ErrorResponse"; // Accept a serialized request Mockito.when(handler.apply(Mockito.anyString())).thenCallRealMethod(); // Deserialize it back into a POJO Mockito.when(handler.decode(Mockito.anyString())).thenReturn(request); // Construct the Response for that Request Mockito.when(request.accept(Mockito.nullable(Service.class))).thenReturn(response); // Throw an IOException when serializing the Response. Mockito.when(handler.encode(response)).thenThrow(exception); Mockito.when(handler.convertToErrorResponse(exception)).thenCallRealMethod(); // Convert the IOException into an ErrorResponse Mockito.when(handler.unwrapException(exception)).thenReturn(errorResponse); Mockito.when(handler.encode(errorResponse)).thenReturn(serializedErrorResponse); HandlerResponse<String> handlerResp = handler.apply("this is mocked out"); assertEquals(500, handlerResp.getStatusCode()); assertEquals(serializedErrorResponse, handlerResp.getResponse()); }
@Test public void testFailedResponseSerialization() throws IOException { @SuppressWarnings("unchecked") final AbstractHandler<String> handler = Mockito.mock(AbstractHandler.class); final Request request = Mockito.mock(Request.class); final Response response = Mockito.mock(Response.class); final IOException exception = new IOException(); final ErrorResponse errorResponse = Mockito.mock(ErrorResponse.class); final String serializedErrorResponse = "An ErrorResponse"; // Accept a serialized request Mockito.when(handler.apply(Mockito.anyString())).thenCallRealMethod(); // Deserialize it back into a POJO Mockito.when(handler.decode(Mockito.anyString())).thenReturn(request); // Construct the Response for that Request Mockito.when(request.accept(Mockito.nullable(Service.class))).thenReturn(response); // Throw an IOException when serializing the Response. Mockito.when(handler.encode(response)).thenThrow(exception); Mockito.when(handler.convertToErrorResponse(exception)).thenCallRealMethod(); // Convert the IOException into an ErrorResponse Mockito.when(handler.unwrapException(exception)).thenReturn(errorResponse); Mockito.when(handler.encode(errorResponse)).thenReturn(serializedErrorResponse); HandlerResponse<String> handlerResp = handler.apply("this is mocked out"); assertEquals(500, handlerResp.getStatusCode()); assertEquals(serializedErrorResponse, handlerResp.getResponse()); }
Mockito.nullable(LogLevel.class), Mockito.anyString(), Mockito.anyInt());
Mockito.when(testCampaign.getTechs()).thenCallRealMethod(); Mockito.when(testCampaign.getTechs(Mockito.anyBoolean())).thenCallRealMethod(); Mockito.when(testCampaign.getTechs(Mockito.anyBoolean(), Mockito.nullable(UUID.class), Mockito.anyBoolean(), Mockito.anyBoolean())).thenCallRealMethod(); Mockito.when(testCampaign.getPerson(Mockito.eq(testId))).thenReturn(mockTechActiveTwo);
Mockito.doReturn(10.0).when(testRanker).getMaxDamageAtRange(Mockito.nullable(FireControl.class), Mockito.eq(mockMyUnit), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean()); Mockito.doReturn(8.5) .when(testRanker) .getMaxDamageAtRange(Mockito.nullable(FireControl.class), Mockito.eq(mockEnemyMech), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean()); Mockito.doReturn(false) Mockito.doReturn(8.5) .when(testRanker) .getMaxDamageAtRange(Mockito.nullable(FireControl.class), Mockito.eq(mockEnemyMech), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean()); Mockito.doReturn(false) Mockito.doReturn(8.5) .when(testRanker) .getMaxDamageAtRange(Mockito.nullable(FireControl.class), Mockito.eq(mockEnemyMech), Mockito.anyInt(), Mockito.anyBoolean(), Mockito.anyBoolean()); Mockito.doReturn(true)
.when(testFireControl) .getAttackerMovementModifier(Mockito.any(IGame.class), Mockito.anyInt(), Mockito.nullable(EntityMovementType.class)); Mockito.doReturn(mockTargetMoveMod) .when(testFireControl)
Mockito.doReturn(3) .when(boardCenter) .direction(Mockito.nullable(Coords.class)); Mockito.doReturn(3) .when(enemyMech1Position) .direction(Mockito.nullable(Coords.class)); final Entity mockEnemyMech1 = Mockito.mock(BipedMech.class); Mockito.when(mockEnemyMech1.isOffBoard()).thenReturn(false); Mockito.doReturn(mockEnemyMech1) .when(testRanker) .findClosestEnemy(Mockito.eq(mockMover), Mockito.nullable(Coords.class), Mockito.any(IGame.class)); .distanceToHomeEdge(Mockito.nullable(Coords.class), Mockito.any(CardinalEdge.class), Mockito.any(IGame.class)); Mockito.when(mockPrincess.wantsToFallBack(Mockito.eq(mockMover))).thenReturn(false); Mockito.when(mockMover.isCrippled()).thenReturn(false); .findClosestEnemy(Mockito.eq(mockMover), Mockito.nullable(Coords.class), Mockito.any(IGame.class)); expected = new RankedPath(-51.25, mockPath, "Calculation: " + "{fall mod [" + LOG_DECIMAL.format(0) + " = " + LOG_DECIMAL Mockito.doReturn(mockEnemyMech1) .when(testRanker) .findClosestEnemy(Mockito.eq(mockMover), Mockito.nullable(Coords.class), Mockito.any(IGame.class));
@Test public void testHostnameVerification() throws Exception { AvaticaCommonsHttpClientImpl client = mock(AvaticaCommonsHttpClientImpl.class); // Call the real method when(client.getHostnameVerifier(nullable(HostnameVerification.class))) .thenCallRealMethod(); // No verification should give the default (strict) verifier HostnameVerifier actualVerifier = client.getHostnameVerifier(null); assertNotNull(actualVerifier); assertTrue(actualVerifier instanceof DefaultHostnameVerifier); actualVerifier = client.getHostnameVerifier(HostnameVerification.STRICT); assertNotNull(actualVerifier); assertTrue(actualVerifier instanceof DefaultHostnameVerifier); actualVerifier = client.getHostnameVerifier(HostnameVerification.NONE); assertNotNull(actualVerifier); assertTrue(actualVerifier instanceof NoopHostnameVerifier); } }
@Test public void testHostnameVerification() throws Exception { AvaticaCommonsHttpClientImpl client = mock(AvaticaCommonsHttpClientImpl.class); // Call the real method when(client.getHostnameVerifier(nullable(HostnameVerification.class))) .thenCallRealMethod(); // No verification should give the default (strict) verifier HostnameVerifier actualVerifier = client.getHostnameVerifier(null); assertNotNull(actualVerifier); assertTrue(actualVerifier instanceof DefaultHostnameVerifier); actualVerifier = client.getHostnameVerifier(HostnameVerification.STRICT); assertNotNull(actualVerifier); assertTrue(actualVerifier instanceof DefaultHostnameVerifier); actualVerifier = client.getHostnameVerifier(HostnameVerification.NONE); assertNotNull(actualVerifier); assertTrue(actualVerifier instanceof NoopHostnameVerifier); } }
testPhysicalInfo.setTarget(mockTarget); Mockito.doNothing().when(testPhysicalInfo).setDamageDirection(Mockito.any(EntityState.class), Mockito.nullable(Coords.class)); Mockito.doReturn(1).when(testPhysicalInfo).getDamageDirection();