Skip to content

Instantly share code, notes, and snippets.

@ihommani
Created November 1, 2015 18:27
Show Gist options
  • Select an option

  • Save ihommani/b122e6fa67948404db45 to your computer and use it in GitHub Desktop.

Select an option

Save ihommani/b122e6fa67948404db45 to your computer and use it in GitHub Desktop.
Typical test using tesng with mockito
public class SharePointStreamFetcherTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
SPDataLoaderManager spDataLoaderManager;
SharePointStreamFetcher underTest;
SoftAssertions softAssertions = new SoftAssertions();
@BeforeMethod
public void setUp() throws Exception {
MockitoAnnotations.initMocks(spDataLoaderManager);
underTest = new SharePointStreamFetcher(spDataLoaderManager, "siteUrl", "listId");
}
@AfterMethod
public void tearDown() throws Exception {
softAssertions.assertAll();
// specifick mock to forget each previous stubbing.
reset(spDataLoaderManager);
}
@Test
public void should_do_some_testing() throws IOException, JAXBException {
// given
ItemIndex itemIndex = new ItemIndex();
// ... setting some stuff
when(spDataLoaderManager.getItemIndex(anyString(), anyString(), anyInt(), anyString())).thenReturn(itemIndex);
// when
Stream<ItemIndex> itemIndexStream = underTest.getItemIndexStream();
List<ItemIndex> toTestValue = itemIndexStream.collect(Collectors.toList());
// then
softAssertions.assertThat(toTestValue).isNotEmpty().hasSize(42).contains(new ItemIndex());
verify(spDataLoaderManager, atLeastOnce()).getItemIndex(anyString(), anyString(), anyInt(), anyString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment