use of org.mockito.ArgumentCaptor in project java-design-patterns by iluwatar.
the class DispatcherTest method testMenuItemSelected.
@Test
public void testMenuItemSelected() throws Exception {
final Dispatcher dispatcher = Dispatcher.getInstance();
final Store store = mock(Store.class);
dispatcher.registerStore(store);
dispatcher.menuItemSelected(MenuItem.HOME);
dispatcher.menuItemSelected(MenuItem.COMPANY);
// We expect 4 events, 2 menu selections and 2 content change actions
final ArgumentCaptor<Action> actionCaptor = ArgumentCaptor.forClass(Action.class);
verify(store, times(4)).onAction(actionCaptor.capture());
verifyNoMoreInteractions(store);
final List<Action> actions = actionCaptor.getAllValues();
final List<MenuAction> menuActions = actions.stream().filter(a -> a.getType().equals(ActionType.MENU_ITEM_SELECTED)).map(a -> (MenuAction) a).collect(Collectors.toList());
final List<ContentAction> contentActions = actions.stream().filter(a -> a.getType().equals(ActionType.CONTENT_CHANGED)).map(a -> (ContentAction) a).collect(Collectors.toList());
assertEquals(2, menuActions.size());
assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.HOME::equals).count());
assertEquals(1, menuActions.stream().map(MenuAction::getMenuItem).filter(MenuItem.COMPANY::equals).count());
assertEquals(2, contentActions.size());
assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.PRODUCTS::equals).count());
assertEquals(1, contentActions.stream().map(ContentAction::getContent).filter(Content.COMPANY::equals).count());
}
use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.
the class DeleteActionTest method request_also_deletes_components_of_specified_organization.
@Test
public void request_also_deletes_components_of_specified_organization() {
OrganizationDto organization = dbTester.organizations().insert();
ComponentDto project = dbTester.components().insertProject(organization);
ComponentDto module = dbTester.components().insertComponent(ComponentTesting.newModuleDto(project));
ComponentDto directory = dbTester.components().insertComponent(ComponentTesting.newDirectory(module, "a/b"));
ComponentDto file = dbTester.components().insertComponent(ComponentTesting.newFileDto(module, directory));
ComponentDto view = dbTester.components().insertView(organization, (dto) -> {
});
ComponentDto subview1 = dbTester.components().insertComponent(ComponentTesting.newSubView(view, "v1", "ksv1"));
ComponentDto subview2 = dbTester.components().insertComponent(ComponentTesting.newSubView(subview1, "v2", "ksv2"));
ComponentDto projectCopy = dbTester.components().insertComponent(ComponentTesting.newProjectCopy("pc1", project, subview1));
logInAsAdministrator(organization);
sendRequest(organization);
verifyOrganizationDoesNotExist(organization);
ArgumentCaptor<List<ComponentDto>> arg = (ArgumentCaptor<List<ComponentDto>>) ((ArgumentCaptor) ArgumentCaptor.forClass(List.class));
verify(componentCleanerService).delete(any(DbSession.class), arg.capture());
assertThat(arg.getValue()).containsOnly(project, view);
}
use of org.mockito.ArgumentCaptor in project Shuttle by timusus.
the class ShuttleUtilsTest method testIncrementPlayCount.
@Test
public void testIncrementPlayCount() throws Exception {
Context mockContext = mock(Context.class);
ContentResolver mockContentResolver = mock(ContentResolver.class);
Song fakeSong = new Song(mock(Cursor.class));
fakeSong.id = 100L;
fakeSong.playCount = 50;
// getPlayCount has extra logic to conduct SQL Queries, which would be a pain to mock in a test
// so, we can use a Spy here in order to use both fake data (above) plus control return behaviors (below)
Song spySong = spy(fakeSong);
doReturn(50).when(spySong).getPlayCount(any(Context.class));
when(mockContext.getContentResolver()).thenReturn(mockContentResolver);
// Setup to perform the method call on a song with no play counts
when(mockContentResolver.update(any(Uri.class), any(ContentValues.class), anyString(), any(String[].class))).thenReturn(0);
// Call the method and capture the ContentValues object
ArgumentCaptor<ContentValues> contentValuesCaptor = new ArgumentCaptor<>();
ShuttleUtils.incrementPlayCount(mockContext, spySong);
verify(mockContentResolver).update(any(Uri.class), contentValuesCaptor.capture(), anyString(), any(String[].class));
// Check that the values being updated or inserted match the actual song
ContentValues values = contentValuesCaptor.getValue();
assertThat(values.get("_id")).isEqualTo(100L);
assertThat(values.get("play_count")).isEqualTo(51);
verify(mockContentResolver).insert(any(Uri.class), eq(values));
// Next, test what happens with a song that already had play counts (should not cause an insert operation)
reset(mockContentResolver);
when(mockContentResolver.update(any(Uri.class), contentValuesCaptor.capture(), anyString(), any(String[].class))).thenReturn(1);
ShuttleUtils.incrementPlayCount(mockContext, spySong);
verify(mockContentResolver, never()).insert(any(Uri.class), any(ContentValues.class));
}
use of org.mockito.ArgumentCaptor in project Shuttle by timusus.
the class ShuttleUtilsTest method testOpenShuttleLinkWebLink.
@Test
public void testOpenShuttleLinkWebLink() throws Exception {
Activity mockActivity = mock(Activity.class);
String fakePackage = "fake.package.name";
// Setup the mock to throw an ActivityNotFoundException only the first time startActivity is called
doThrow(new ActivityNotFoundException()).doNothing().when(mockActivity).startActivity(any(Intent.class));
// Call the method and capture the "fired" intent
ShuttleUtils.openShuttleLink(mockActivity, fakePackage);
ArgumentCaptor<Intent> intentCaptor = new ArgumentCaptor<>();
verify(mockActivity, times(2)).startActivity(intentCaptor.capture());
Intent intent = intentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
// Also, I guess ShuttleUtils.isAmazonBuild() needs to be tested or we can't trust this test
if (ShuttleUtils.isAmazonBuild()) {
assertThat(intent.getData()).isEqualTo(Uri.parse("http://www.amazon.com/gp/mas/dl/android?p=" + fakePackage));
} else {
assertThat(intent.getData()).isEqualTo(Uri.parse("https://play.google.com/store/apps/details?id=" + fakePackage));
}
}
use of org.mockito.ArgumentCaptor in project OpenAM by OpenRock.
the class RestAuthenticationHandlerTest method shouldInitiateAuthenticationViaGET1.
@Test
public void shouldInitiateAuthenticationViaGET1() throws AuthLoginException, L10NMessageImpl, JSONException, IOException, RestAuthException, RestAuthResponseException {
//Given
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse httpResponse = mock(HttpServletResponse.class);
String authIndexType = AuthIndexType.MODULE.toString();
String indexValue = "INDEX_VALUE";
String sessionUpgradeSSOTokenId = null;
AuthContextLocalWrapper authContextLocalWrapper = mock(AuthContextLocalWrapper.class);
given(authContextLocalWrapper.getErrorCode()).willReturn("ERROR_CODE");
given(authContextLocalWrapper.getErrorMessage()).willReturn("ERROR_MESSAGE");
LoginProcess loginProcess = mock(LoginProcess.class);
given(loginProcess.getLoginStage()).willReturn(LoginStage.COMPLETE);
given(loginProcess.isSuccessful()).willReturn(false);
given(loginProcess.getAuthContext()).willReturn(authContextLocalWrapper);
given(loginAuthenticator.getLoginProcess(Matchers.<LoginConfiguration>anyObject())).willReturn(loginProcess);
//When
try {
restAuthenticationHandler.initiateAuthentication(request, httpResponse, authIndexType, indexValue, sessionUpgradeSSOTokenId);
} catch (RestAuthErrorCodeException e) {
assertEquals(e.getStatusCode(), 401);
ArgumentCaptor<LoginConfiguration> argumentCaptor = ArgumentCaptor.forClass(LoginConfiguration.class);
verify(loginAuthenticator).getLoginProcess(argumentCaptor.capture());
LoginConfiguration loginConfiguration = argumentCaptor.getValue();
assertEquals(loginConfiguration.getHttpRequest(), request);
assertEquals(loginConfiguration.getIndexType(), AuthIndexType.MODULE);
assertEquals(loginConfiguration.getIndexValue(), "INDEX_VALUE");
assertEquals(loginConfiguration.getSessionId(), "");
assertEquals(loginConfiguration.getSSOTokenId(), "");
return;
}
//Then
fail();
}
Aggregations