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