### Question:
ApplicationModelFactory { public ApplicationModel createApplicationModel(final String name) throws CreationException { try { final SearchResult<Application> result = applicationApi.searchApplications( new SearchOptionsBuilder(0, 1) .filter(ApplicationSearchDescriptor.TOKEN, name) .done()); if (result.getCount() == 0) { throw new CreationException("No application found with name " + name); } return new ApplicationModel( applicationApi, customPageApi, profileApi, result.getResult().get(0), new MenuFactory(applicationApi)); } catch (final SearchException e) { throw new CreationException("Error while searching for the application " + name, e); } } ApplicationModelFactory(final ApplicationAPI applicationApi, final PageAPI customPageApi, final ProfileAPI profileApi); ApplicationModel createApplicationModel(final String name);  }### Answer:
@Test(expected = CreationException.class) public void should_throw_create_error_exception_when_search_fail() throws Exception { given(applicationApi.searchApplications(any(SearchOptions.class))).willThrow(SearchException.class); factory.createApplicationModel("foo"); }
@Test(expected = CreationException.class) public void should_throw_create_error_exception_when_application_is_not_found() throws Exception { given(applicationApi.searchApplications(any(SearchOptions.class))).willReturn( new SearchResultImpl<Application>(0, Collections.<Application> emptyList())); factory.createApplicationModel("foo"); }
@Test public void should_return_application_found() throws Exception { final ApplicationImpl application = new ApplicationImpl("foobar", "1.0", "bazqux"); application.setId(3); given(applicationApi.searchApplications(any(SearchOptions.class))).willReturn( new SearchResultImpl<Application>(1, asList((Application) application))); given(applicationApi.getApplicationHomePage(3)).willReturn(new ApplicationPageImpl(1, 1, "home")); final ApplicationModel model = factory.createApplicationModel("foo"); assertThat(model.getApplicationHomePage()).isEqualTo("home/"); }
@Test public void should_filter_search_using_given_name() throws Exception { final ArgumentCaptor<SearchOptions> captor = ArgumentCaptor.forClass(SearchOptions.class); given(applicationApi.searchApplications(any(SearchOptions.class))).willReturn( new SearchResultImpl<Application>(1, asList(mock(Application.class)))); factory.createApplicationModel("bar"); verify(applicationApi).searchApplications(captor.capture()); final SearchFilter filter = captor.getValue().getFilters().get(0); assertThat(filter.getField()).isEqualTo("token"); assertThat(filter.getValue()).isEqualTo("bar"); }