1 /* 2 * Copyright (C) 2024 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 */ 16 package com.android.adservices.shared.testing.mockito; 17 18 import static org.junit.Assert.assertThrows; 19 import static org.mockito.Mockito.any; 20 import static org.mockito.Mockito.doAnswer; 21 22 import com.android.adservices.shared.testing.Nullable; 23 import com.android.adservices.shared.testing.SidelessSharedMockitoTestCase; 24 25 import org.junit.Test; 26 import org.mockito.Mock; 27 import org.mockito.invocation.InvocationOnMock; 28 import org.mockito.stubbing.Answer; 29 30 @SuppressWarnings("DirectInvocationOnMock") // is testing mocking stuff 31 public final class MockitoHelperTest extends SidelessSharedMockitoTestCase { 32 33 // Used to get a "real" InvocationOnMock 34 private final VoidAnswer mAnswer = new VoidAnswer(); 35 36 @Mock private Fixture mFixture; 37 38 @Mock private InvocationOnMock mMockInvocation; 39 40 @Test testToString_null()41 public void testToString_null() { 42 assertThrows(NullPointerException.class, () -> MockitoHelper.toString(null)); 43 } 44 45 @Test testToString_mockInvocation()46 public void testToString_mockInvocation() { 47 String toString = MockitoHelper.toString(mMockInvocation); 48 49 expect.withMessage("MockitoHelper.toString(%s)", mMockInvocation) 50 .that(toString) 51 .isEqualTo("mMockInvocation"); 52 } 53 54 @Test testToString_oneArgument()55 public void testToString_oneArgument() { 56 doAnswer(mAnswer).when(mFixture).pEquals(any()); 57 mFixture.pEquals("NP"); 58 59 String toString = MockitoHelper.toString(mAnswer.invocation); 60 61 expect.withMessage("MockitoHelper.toString(%s)", mAnswer.invocation) 62 .that(toString) 63 .isEqualTo("pEquals(NP)"); 64 } 65 66 @Test testToString_oneArgument_null()67 public void testToString_oneArgument_null() { 68 doAnswer(mAnswer).when(mFixture).pEquals(any()); 69 mFixture.pEquals(null); 70 71 String toString = MockitoHelper.toString(mAnswer.invocation); 72 73 expect.withMessage("MockitoHelper.toString(%s)", mAnswer.invocation) 74 .that(toString) 75 .isEqualTo("pEquals(null)"); 76 } 77 78 @Test testToString_multipleArguments()79 public void testToString_multipleArguments() { 80 doAnswer(mAnswer).when(mFixture).toInfinity(any(), any()); 81 mFixture.toInfinity("and", "beyond"); 82 83 String toString = MockitoHelper.toString(mAnswer.invocation); 84 85 expect.withMessage("MockitoHelper.toString(%s)", mAnswer.invocation) 86 .that(toString) 87 .isEqualTo("toInfinity(and, beyond)"); 88 } 89 90 @Test testToString_multipleArguments_null()91 public void testToString_multipleArguments_null() { 92 doAnswer(mAnswer).when(mFixture).toInfinity(any(), any()); 93 mFixture.toInfinity("and", null); 94 95 String toString = MockitoHelper.toString(mAnswer.invocation); 96 97 expect.withMessage("MockitoHelper.toString(%s)", mAnswer.invocation) 98 .that(toString) 99 .isEqualTo("toInfinity(and, null)"); 100 } 101 102 @Test testToString_varArgs()103 public void testToString_varArgs() { 104 doAnswer(mAnswer).when(mFixture).iJustCalled(any()); 105 mFixture.iJustCalled("", "to", "say", ",", "I", "love", "you", "!"); 106 107 String toString = MockitoHelper.toString(mAnswer.invocation); 108 109 expect.withMessage("MockitoHelper.toString(%s)", mAnswer.invocation) 110 .that(toString) 111 .isEqualTo("iJustCalled(, to, say, ,, I, love, you, !)"); 112 } 113 114 @Test testToString_nullVarArgs()115 public void testToString_nullVarArgs() { 116 doAnswer(mAnswer).when(mFixture).iJustCalled(any()); 117 mFixture.iJustCalled((Object[]) null); 118 119 String toString = MockitoHelper.toString(mAnswer.invocation); 120 121 expect.withMessage("MockitoHelper.toString(%s)", mAnswer.invocation) 122 .that(toString) 123 .isEqualTo("iJustCalled(null)"); 124 } 125 126 @Test testToString_varArgsWithNull()127 public void testToString_varArgsWithNull() { 128 doAnswer(mAnswer).when(mFixture).iJustCalled(any()); 129 mFixture.iJustCalled("", "to", "say", ",", "I", null, "you", "!"); 130 131 String toString = MockitoHelper.toString(mAnswer.invocation); 132 133 expect.withMessage("MockitoHelper.toString(%s)", mAnswer.invocation) 134 .that(toString) 135 .isEqualTo("iJustCalled(, to, say, ,, I, null, you, !)"); 136 } 137 138 private static final class VoidAnswer implements Answer<Void> { 139 @Nullable public InvocationOnMock invocation; 140 141 @Override answer(InvocationOnMock invocation)142 public Void answer(InvocationOnMock invocation) throws Throwable { 143 this.invocation = invocation; 144 return null; 145 } 146 } 147 148 interface Fixture { voidTheVoidToAVoidVoidances()149 void voidTheVoidToAVoidVoidances(); 150 pEquals(String arg)151 void pEquals(String arg); 152 toInfinity(String arg1, String arg2)153 void toInfinity(String arg1, String arg2); 154 iJustCalled(Object... args)155 void iJustCalled(Object... args); 156 } 157 } 158