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