How to use UnusedStubbingsFinder class of org.mockito.internal.junit package

Best Mockito code snippet using org.mockito.internal.junit.UnusedStubbingsFinder

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright (c) 2017 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockitousage.internal.junit;
6
7import org.junit.Test;
8import org.mockito.Mock;
9import org.mockito.internal.junit.UnusedStubbings;
10import org.mockito.internal.junit.UnusedStubbingsFinder;
11import org.mockitousage.IMethods;
12import org.mockitoutil.TestBase;
13
14import java.util.Collection;
15import java.util.List;
16
17import static java.util.Arrays.asList;
18import static org.junit.Assert.assertEquals;
19import 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 */
27public 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
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on UnusedStubbingsFinder

    No relevant questions found for this class 😞

Most used methods in UnusedStubbingsFinder

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)