Advertisement
Guest User

Mockito problem

a guest
Jul 26th, 2010
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package dk.skat.dmr.service.business;
  2.  
  3. import static org.junit.Assert.*;
  4. import static org.mockito.Mockito.*;
  5. import static org.mockito.Matchers.*;
  6.  
  7. import java.util.List;
  8.  
  9. import org.junit.Test;
  10. import org.mockito.ArgumentCaptor;
  11. import org.mockito.InOrder;
  12.  
  13. public class ArgCaptorTest {
  14.  
  15.     @Test
  16.     public void testAdd() {
  17.         ArgumentCaptor<Dummy> captor = ArgumentCaptor.forClass(Dummy.class);
  18.  
  19.         List<Dummy> mList = mock(List.class);
  20.         Dummy dummy = new Dummy();
  21.         when(mList.get(anyInt())).thenReturn(dummy);
  22.  
  23.         Dummy d = mList.get(12);
  24.         d.setName("John");
  25.         mList.add(d);
  26.  
  27.         Dummy g = mList.get(10);
  28.         g.setName("Ben");
  29.         mList.add(g);
  30.  
  31.         Dummy x = mList.get(120);
  32.         x.setName("Don");
  33.         mList.add(x);
  34.  
  35.         verify(mList, times(3)).add(captor.capture());
  36.  
  37.         // This doesnt work either
  38.         // InOrder inOrder = inOrder(mList);
  39.         // inOrder.verify(mList).add(captor.capture());
  40.         // inOrder.verify(mList).add(captor.capture());
  41.         // inOrder.verify(mList).add(captor.capture());
  42.  
  43.         assertEquals("John", captor.getAllValues().get(0).getName());
  44.         assertEquals("Ben", captor.getAllValues().get(1).getName());
  45.         assertEquals("Don", captor.getAllValues().get(2).getName());
  46.  
  47.     }
  48.  
  49.     class Dummy {
  50.         private String name;
  51.         private int id;
  52.  
  53.         public String getName() {
  54.             return name;
  55.         }
  56.  
  57.         public void setName(String name) {
  58.             this.name = name;
  59.         }
  60.  
  61.         public int getId() {
  62.             return id;
  63.         }
  64.  
  65.         public void setId(int id) {
  66.             this.id = id;
  67.         }
  68.  
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement