How to use method of org.mockito.ArgumentMatchers class

Best Mockito code snippet using org.mockito.ArgumentMatchers.

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.helper.aoa;
17
18import static org.junit.Assert.assertFalse;
19import static org.junit.Assert.assertTrue;
20import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.ArgumentMatchers.anyByte;
22import static org.mockito.ArgumentMatchers.anyInt;
23import static org.mockito.ArgumentMatchers.anyShort;
24import static org.mockito.ArgumentMatchers.eq;
25import static org.mockito.Mockito.mock;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
29import com.sun.jna.Memory;
30import com.sun.jna.Pointer;
31import com.sun.jna.ptr.PointerByReference;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.junit.runners.JUnit4;
37
38/** Unit tests for {@link UsbDevice} */
39@RunWith(JUnit4.class)
40public class UsbDeviceTest {
41
42    private UsbDevice mDevice;
43
44    private Pointer mHandle;
45    private IUsbNative mUsb;
46
47    @Before
48    public void setUp() {
49        // create dummy device handle
50        mHandle = new Memory(1);
51
52        mUsb = mock(IUsbNative.class);
53        // return device handle when opening connection
54        when(mUsb.libusb_open(any(), any()))
55                .then(
56                        invocation -> {
57                            PointerByReference handle =
58                                    (PointerByReference) invocation.getArguments()[1];
59                            handle.setValue(mHandle);
60                            return 0;
61                        });
62
63        mDevice = new UsbDevice(mUsb, mock(Pointer.class));
64    }
65
66    @Test
67    public void testIsValid() {
68        // has device handle initially
69        assertTrue(mDevice.isValid());
70
71        // device handle invalid after closing connection
72        mDevice.close();
73        assertFalse(mDevice.isValid());
74    }
75
76    @Test
77    public void testControlTransfer() {
78        byte[] data = new byte[]{1, 2, 3, 4};
79        mDevice.controlTransfer((byte) 1, (byte) 2, 3, 4, data);
80
81        // passes right arguments to libusb
82        verify(mUsb).libusb_control_transfer(eq(mHandle),
83                eq((byte) 1), eq((byte) 2), eq((short) 3), eq((short) 4),
84                eq(data), eq((short) 4), // data and length
85                eq(0)); // timeout
86    }
87
88    @Test(expected = NullPointerException.class)
89    public void testControlTransfer_closed() {
90        mDevice.close();
91        // trying to operate on a closed handle should fail
92        mDevice.controlTransfer((byte) 1, (byte) 2, 3, 4, new byte[0]);
93    }
94
95    @Test
96    public void testReset() {
97        mDevice.reset();
98        verify(mUsb).libusb_reset_device(eq(mHandle));
99    }
100
101    @Test(expected = NullPointerException.class)
102    public void testReset_closed() {
103        mDevice.close();
104        // trying to operate on a closed handle should fail
105        mDevice.reset();
106    }
107
108    @Test
109    public void testIsAoaCompatible() {
110        // supports AOA protocol version 2
111        when(mUsb.libusb_control_transfer(
112                        eq(mHandle),
113                        anyByte(),
114                        anyByte(),
115                        anyShort(),
116                        anyShort(),
117                        any(),
118                        anyShort(),
119                        anyInt()))
120                .thenReturn(2);
121        assertTrue(mDevice.isAoaCompatible());
122
123        // does not support any AOA protocol
124        when(mUsb.libusb_control_transfer(
125                        eq(mHandle),
126                        anyByte(),
127                        anyByte(),
128                        anyShort(),
129                        anyShort(),
130                        any(),
131                        anyShort(),
132                        anyInt()))
133                .thenReturn(-1);
134        assertFalse(mDevice.isAoaCompatible());
135    }
136}
137
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

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)