How to use method of org.mockito.errorprone.bugpatterns.MockitoAnyIncorrectPrimitiveTypeTest class

Best Mockito code snippet using org.mockito.errorprone.bugpatterns.MockitoAnyIncorrectPrimitiveTypeTest.

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.mockito.errorprone.bugpatterns;
6
7import com.google.errorprone.BugCheckerRefactoringTestHelper;
8import com.google.errorprone.CompilationTestHelper;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.junit.runners.JUnit4;
13
14/**
15 * Verify that there are no Mockito `any(Class)` matchers for arguments that are primitives.
16 *
17 * @author tvanderlippe@google.com (Tim van der Lippe)
18 */
19@RunWith(JUnit4.class)
20public class MockitoAnyIncorrectPrimitiveTypeTest {
21
22  private CompilationTestHelper compilationHelper;
23  private BugCheckerRefactoringTestHelper refactoringHelper;
24
25  @Before
26  public void setUp() {
27    compilationHelper =
28        CompilationTestHelper.newInstance(MockitoAnyIncorrectPrimitiveType.class, getClass());
29    refactoringHelper =
30        BugCheckerRefactoringTestHelper.newInstance(
31            new MockitoAnyIncorrectPrimitiveType(), getClass());
32  }
33
34  @Test
35  public void testPositiveCases() {
36    compilationHelper
37        .addSourceLines(
38            "Test.java",
39            "package org.mockito;",
40            "import static org.mockito.ArgumentMatchers.any;",
41            "import static org.mockito.ArgumentMatchers.anyInt;",
42            "import static org.mockito.Mockito.mock;",
43            "import static org.mockito.Mockito.when;",
44            "class Test {",
45            "  public void test() {",
46            "    Foo foo = mock(Foo.class);",
47            "    // BUG: Diagnostic contains:",
48            "    when(foo.run(anyInt())).thenReturn(5);",
49            "    // BUG: Diagnostic contains:",
50            "    when(foo.runWithBoth(any(String.class), anyInt())).thenReturn(5);",
51            "  }",
52            "  static class Foo {",
53            "    int run(long arg) {",
54            "      return 42;",
55            "    }",
56            "    int runWithBoth(String arg1, long arg2) {",
57            "      return 42;",
58            "    }",
59            "  }",
60            "}")
61        .doTest();
62  }
63
64  @Test
65  public void testNegativeCases() {
66    compilationHelper
67        .addSourceLines(
68            "Test.java",
69            "package org.mockito;",
70            "import static org.mockito.ArgumentMatchers.any;",
71            "import static org.mockito.ArgumentMatchers.anyFloat;",
72            "import static org.mockito.ArgumentMatchers.anyLong;",
73            "import static org.mockito.Mockito.mock;",
74            "import static org.mockito.Mockito.when;",
75            "class Test {",
76            "  public void test() {",
77            "    Foo foo = mock(Foo.class);",
78            "    when(foo.runWithInt(anyFloat())).thenReturn(5);",
79            "    when(foo.runWithBoth(any(String.class), anyLong())).thenReturn(5);",
80            "  }",
81            "  static class Foo {",
82            "    int run(String arg) {",
83            "      return 42;",
84            "    }",
85            "    int runWithInt(float arg) {",
86            "      return 42;",
87            "    }",
88            "    int runWithBoth(String arg1, long arg2) {",
89            "      return 42;",
90            "    }",
91            "  }",
92            "}")
93        .doTest();
94  }
95
96  @Test
97  public void testPositivesSubclass() {
98    compilationHelper
99        .addSourceLines(
100            "Test.java",
101            "package org.mockito;",
102            "import static org.mockito.Mockito.mock;",
103            "import static org.mockito.Mockito.when;",
104            "import static org.mockito.Mockito.any;",
105            "import static org.mockito.Mockito.anyInt;",
106            "class Test {",
107            "  public void test() {",
108            "    Foo foo = mock(Foo.class);",
109            "    // BUG: Diagnostic contains:",
110            "    when(foo.run(anyInt())).thenReturn(5);",
111            "    // BUG: Diagnostic contains:",
112            "    when(foo.runWithBoth(any(String.class), anyInt())).thenReturn(5);",
113            "  }",
114            "  static class Foo {",
115            "    int run(long arg) {",
116            "      return 42;",
117            "    }",
118            "    int runWithBoth(String arg1, float arg2) {",
119            "      return 42;",
120            "    }",
121            "  }",
122            "}")
123        .doTest();
124  }
125
126  @Test
127  public void testRefactoring() {
128    refactoringHelper
129        .addInputLines(
130            "Test.java",
131            "import static org.mockito.ArgumentMatchers.any;",
132            "import static org.mockito.ArgumentMatchers.anyInt;",
133            "import static org.mockito.Mockito.mock;",
134            "import static org.mockito.Mockito.when;",
135            "class Test {",
136            "  public void test() {",
137            "    Foo foo = mock(Foo.class);",
138            "    when(foo.run(anyInt())).thenReturn(5);",
139            "    when(foo.runWithBoth(any(String.class), anyInt())).thenReturn(5);",
140            "  }",
141            "  static class Foo {",
142            "    int run(float arg) {",
143            "      return 42;",
144            "    }",
145            "    int runWithBoth(String arg1, long arg2) {",
146            "      return 42;",
147            "    }",
148            "  }",
149            "}")
150        .addOutputLines(
151            "Test.java",
152            "import static org.mockito.ArgumentMatchers.any;",
153            "import static org.mockito.ArgumentMatchers.anyFloat;",
154            "import static org.mockito.ArgumentMatchers.anyInt;",
155            "import static org.mockito.ArgumentMatchers.anyLong;",
156            "import static org.mockito.Mockito.mock;",
157            "import static org.mockito.Mockito.when;",
158            "class Test {",
159            "  public void test() {",
160            "    Foo foo = mock(Foo.class);",
161            "    when(foo.run(anyFloat())).thenReturn(5);",
162            "    when(foo.runWithBoth(any(String.class), anyLong())).thenReturn(5);",
163            "  }",
164            "  static class Foo {",
165            "    int run(float arg) {",
166            "      return 42;",
167            "    }",
168            "    int runWithBoth(String arg1, long arg2) {",
169            "      return 42;",
170            "    }",
171            "  }",
172            "}")
173        .doTest();
174  }
175}
176
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 used method in MockitoAnyIncorrectPrimitiveTypeTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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)