How to use misplaced_anyBoolean_argument_matcher method of org.mockitousage.misuse.DetectingMisusedMatchersTest class

Best Mockito code snippet using org.mockitousage.misuse.DetectingMisusedMatchersTest.misplaced_anyBoolean_argument_matcher

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockitousage.misuse;
6
7import org.junit.After;
8import org.junit.Test;
9import org.mockito.Mock;
10import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
11import org.mockito.exceptions.misusing.UnfinishedVerificationException;
12import org.mockitousage.IMethods;
13import org.mockitoutil.TestBase;
14
15import java.util.Observer;
16
17import static org.assertj.core.api.Assertions.assertThat;
18import static org.junit.Assert.fail;
19import static org.junit.Assume.assumeTrue;
20import static org.mockito.Mockito.*;
21
22public class DetectingMisusedMatchersTest extends TestBase {
23
24    class WithFinal {
25        final Object finalMethod(Object object) {
26            return null;
27        }
28    }
29
30    @Mock private WithFinal withFinal;
31
32    @After
33    public void resetState() {
34        super.resetState();
35    }
36
37    private void misplaced_anyObject_argument_matcher() {
38        anyObject();
39    }
40
41    private void misplaced_anyInt_argument_matcher() {
42        anyInt();
43    }
44
45    private void misplaced_anyBoolean_argument_matcher() {
46        anyBoolean();
47    }
48
49    @Test
50    public void should_fail_fast_when_argument_matchers_are_abused() {
51        misplaced_anyObject_argument_matcher();
52        try {
53            mock(IMethods.class);
54            fail();
55        } catch (InvalidUseOfMatchersException e) {
56            assertThat(e).hasMessageContaining("Misplaced or misused argument matcher");
57        }
58    }
59
60    @Test
61    public void should_report_argument_locations_when_argument_matchers_misused() {
62        try {
63            Observer observer = mock(Observer.class);
64
65            misplaced_anyInt_argument_matcher();
66            misplaced_anyObject_argument_matcher();
67            misplaced_anyBoolean_argument_matcher();
68
69            observer.update(null, null);
70
71            validateMockitoUsage();
72            fail();
73        } catch (InvalidUseOfMatchersException e) {
74            assertThat(e)
75                .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyInt_argument_matcher")
76                .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyObject_argument_matcher")
77                .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyBoolean_argument_matcher");
78        }
79    }
80
81
82    @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
83    @Test
84    public void shouldSayUnfinishedVerificationButNotInvalidUseOfMatchers() {
85        assumeTrue("Does not apply for inline mocks", withFinal.getClass() != WithFinal.class);
86        verify(withFinal).finalMethod(anyObject());
87        try {
88            verify(withFinal);
89            fail();
90        } catch (UnfinishedVerificationException e) {}
91    }
92}
93
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 DetectingMisusedMatchersTest

Trigger misplaced_anyBoolean_argument_matcher code on LambdaTest Cloud Grid

Execute automation tests with misplaced_anyBoolean_argument_matcher 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