1 package org.mockitousage.internal.junit; 2 3 import org.junit.Test; 4 import org.mockito.Mock; 5 import org.mockito.internal.junit.UnusedStubbings; 6 import org.mockito.internal.junit.UnusedStubbingsFinder; 7 import org.mockitousage.IMethods; 8 import org.mockitoutil.TestBase; 9 10 import java.util.Collection; 11 import java.util.List; 12 13 import static java.util.Arrays.asList; 14 import static org.junit.Assert.assertEquals; 15 import static org.mockito.Mockito.when; 16 17 /** 18 * This unit test lives in 'org.mockitousage' package for a reason. 19 * It makes it easy to write tests that depend on the stack trace filtering logic. 20 * Long term, it would be nice to have more configurability in TestBase 21 * to make it easy to write such unit tests in 'org.mockito.*' packages. 22 */ 23 public class UnusedStubbingsFinderTest extends TestBase { 24 25 UnusedStubbingsFinder finder = new UnusedStubbingsFinder(); 26 @Mock IMethods mock1; 27 @Mock IMethods mock2; 28 29 @Test no_interactions()30 public void no_interactions() throws Exception { 31 //expect 32 assertEquals(0, finder.getUnusedStubbings((List) asList(mock1, mock2)).size()); 33 assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size()); 34 } 35 36 @Test no_stubbings()37 public void no_stubbings() throws Exception { 38 //when 39 mock1.simpleMethod(); 40 41 //then 42 assertEquals(0, finder.getUnusedStubbings((List) asList(mock1, mock2)).size()); 43 assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size()); 44 } 45 46 @Test no_unused_stubbings()47 public void no_unused_stubbings() throws Exception { 48 //when 49 when(mock1.simpleMethod()).thenReturn("1"); 50 mock1.simpleMethod(); 51 52 //then 53 assertEquals(0, finder.getUnusedStubbings((List) asList(mock1, mock2)).size()); 54 assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size()); 55 } 56 57 @Test unused_stubbings()58 public void unused_stubbings() throws Exception { 59 //when 60 when(mock1.simpleMethod()).thenReturn("1"); 61 62 //then 63 assertEquals(1, finder.getUnusedStubbings((List) asList(mock1, mock2)).size()); 64 assertEquals(1, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size()); 65 } 66 67 @Test some_unused_stubbings()68 public void some_unused_stubbings() throws Exception { 69 when(mock1.simpleMethod(1)).thenReturn("1"); 70 when(mock2.simpleMethod(2)).thenReturn("2"); 71 when(mock2.simpleMethod(3)).thenReturn("3"); 72 73 mock2.simpleMethod(2); 74 75 //when 76 UnusedStubbings stubbings = finder.getUnusedStubbings((List) asList(mock1, mock2)); 77 78 //then 79 assertEquals(2, stubbings.size()); 80 assertEquals("[mock1.simpleMethod(1); stubbed with: [Returns: 1], mock2.simpleMethod(3); stubbed with: [Returns: 3]]", 81 stubbings.toString()); 82 } 83 84 @Test some_unused_stubbings_by_location()85 public void some_unused_stubbings_by_location() throws Exception { 86 when(mock1.simpleMethod(1)).thenReturn("1"); 87 when(mock2.simpleMethod(2)).thenReturn("2"); 88 when(mock2.simpleMethod(3)).thenReturn("3"); 89 90 mock2.simpleMethod(2); 91 92 //when 93 Collection stubbings = finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)); 94 95 //then 96 assertEquals(2, stubbings.size()); 97 assertEquals("[mock1.simpleMethod(1);, mock2.simpleMethod(3);]", stubbings.toString()); 98 } 99 100 @Test stubbing_used_by_location()101 public void stubbing_used_by_location() throws Exception { 102 //when 103 //Emulating stubbing in the same location by putting stubbing in the same line: 104 when(mock1.simpleMethod(1)).thenReturn("1"); when(mock2.simpleMethod(1)).thenReturn("1"); 105 //End of emulation 106 mock1.simpleMethod(1); 107 108 //then technically unused stubbings exist 109 assertEquals(1, finder.getUnusedStubbings((List) asList(mock1, mock2)).size()); 110 //however if we consider stubbings in the same location as the same stubbing, all is used: 111 assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size()); 112 } 113 114 @Test deduplicates_stubbings_by_location()115 public void deduplicates_stubbings_by_location() throws Exception { 116 //when 117 //Emulating stubbing in the same location by putting stubbing in the same line: 118 when(mock1.simpleMethod(1)).thenReturn("1"); when(mock2.simpleMethod(1)).thenReturn("1"); 119 //End of emulation 120 121 //when 122 Collection stubbings = finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)); 123 124 //then 125 assertEquals(1, stubbings.size()); 126 } 127 } 128