Skip to content
Snippets Groups Projects
Unverified Commit 9601af92 authored by Daniele Ceribelli's avatar Daniele Ceribelli
Browse files

Fix file structure, add entry in index and add mocked iterable notes

parent 2395bfbb
No related branches found
No related tags found
No related merge requests found
......@@ -9,3 +9,4 @@ Ecco alcune delle possibilità che ci offre:
- [**Test Double**](./01_test-double.md)
- [**Injection**](./02_injection.md)
- [**Mocked Construction**](./03_mocked-construction.md)
- [**Mocked Iterable**](./04_mocked-iterable.md)
# Mocking di Iterable
Nel caso in cui si volesse mockare un oggetto che implementa l'interfaccia Iterable durante il testing di un altro componente, come si potrebbe procedere?
Inizialmente si potrebbe pensare di sfruttare il `thenReturn` fornendo un iteratore creato ad hoc da noi, ma il problema è che utilizzando `thenReturn` viene creato l'oggetto solo una volta, di conseguenza il test passerebbe solo alla prima iterazione dell'iteratore mockato, dalla seconda in poi si verificherebbe un errore.
Per risolvere questo problema possiamo utilizzare il seguente codice:
```java
public class MockUtils {
@SafeVarargs
public static <T> void whenIterated(Iterable<T> p, T... d) {
when(p.iterator()).thenAnswer((Answer<Iterator<T>>) invocation -> List.of(d).iterator());
}
}
```
In questo caso viene sfruttato il metodo `thenAnswer` che ogni volta permette di creare un nuovo iteratore con l'elemento successivo.
Questa tecnica viene utilizzata quando il SUT che sta venendo testato necessita di iterare su qualcosa per svolgere le sue funzioni.
Ecco un [esempio](https://youtu.be/qYX2cpT30PA?t=2729) di come può essere sfruttata questa strategia:
```java
@Test
void scegliSuccess() {
Giocatore player1 = mock(Giocatore.class);
Giocatore player2 = mock(Giocatore.class);
Giocatore me = mock(Giocatore.class);
when(player1.getMazzettoTop()).thenReturn(Rank.FIVE);
when(player2.getMazzettoTop()).thenReturn(Rank.TEN);
Partita partita = mock(Partita.class);
MockUtils.whenIterated(partita, me, player2, player1);
List<Card> mano = List.of(Card.get(Rank.ACE, Suit.CLUBS), Card.get(Rank.KING, Suit.DIAMONDS), Card.get(Rank.TEN, Suit.HEAR
SelettoreCarta FAILED = mock(SelettoreCarta.class);
SelettoreCarta strategy = new SelettoreRubaMazzetto(FAILED, me);
assertThat(strategy.scegli(mano, partita)).isEqualTo(Card.get(Rank.TEN, Suit.HEARTS));
}
```
\ No newline at end of file
......@@ -117,7 +117,11 @@
- [Spy objects](./10_mocking/01_test-double/04_spy-objects.md)
- [Fake objects](./10_mocking/01_test-double/05_fake-objects.md)
- [Riepilogo](./10_mocking/01_test-double/06_riepilogo.md)
- [Mockito](./10_mocking/01_mockito.md)
- [Mockito](./10_mocking/02_mockito/00_index.md)
- [Implementazione test-double](./10_mocking/02_mockito/01_test-double.md)
- [Injection](./10_mocking/02_mockito/02_injection.md)
- [Mocked constructors](./10_mocking/02_mockito/03_mocked-construction.md)
- [Mocking di Iterable](./10_mocking/02_mockito/04_mocked-iterable.md)
# Verifica e convalida
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment