Skip to content

Instantly share code, notes, and snippets.

@Gabri
Last active December 14, 2020 11:27
Show Gist options
  • Save Gabri/5abd237887998f2d947d37ba7d68a0ea to your computer and use it in GitHub Desktop.
Save Gabri/5abd237887998f2d947d37ba7d68a0ea to your computer and use it in GitHub Desktop.
[JUnit Mockito hamcrest] check unit test vari #mockito #java #junit # hamcrest

JUnit Tests (hamcrest)

Settare un valore in un parametro passato in un mock

Settare il valore ID dell'entità passata come parametro chiamando una dao.insert

doAnswer(invocation -> {
	Object[] args = invocation.getArguments();
	((Masterbook) args[0]).setId(1L);
	return 1L;
}).when(masterbookDAO).insert(any(), any());

Verificare che un metodo venga chiamato

verify(configurationDAO, times(1)).insert(any());

List contiene in qualsiasi ordine

assertThat(itemCodes, containsInAnyOrder(itemCode));

l'oggetto ha la proprietà con un certo valore

assertThat(confOnDb, hasProperty("EnrichmentEdit", not(empty())));

verifcare che oggetto sia diverso da null

assertThat(confOnDb.getEnrichmentEdit(), is(notNullValue()))

verificare che un oggetto sia all'interno di un elemento di una lista

assertThat(results.getBookmark(), is(in(List.of(FactoryUtils.getTagRuleBookmark1().getBookmark(), FactoryUtils.getTagRuleBookmark2().getBookmark()))));

verificare che una lista/iterable non abbia altri valori eccetto quelli indicati

assertThat(set, everyItem(is(oneOf("A", "B", "C"))));

verificare che una lista/iterable non abbia altri valori eccetto quelli indicati nella lista

assertThat(metadataPropertyNames, everyItem(is(in(List.of("itemCode", "title", "brandId")))));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment