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 */ 16 17 package com.android.dx.mockito.inline.extended.tests; 18 19 import android.util.Log; 20 21 import org.junit.Test; 22 import org.mockito.MockitoSession; 23 24 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; 25 import static com.android.dx.mockito.inline.extended.ExtendedMockito.staticMockMarker; 26 import static com.android.dx.mockito.inline.extended.ExtendedMockito.reset; 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertNull; 29 import static org.mockito.Mockito.when; 30 31 32 public class Stress { 33 private static final String LOG_TAG = Stress.class.getSimpleName(); 34 35 private static class SuperClass { returnB()36 static String returnB() { 37 return "superB"; 38 } 39 returnD()40 final static String returnD() { 41 return "superD"; 42 } 43 } 44 45 @Test stressFinalStaticMethod()46 public void stressFinalStaticMethod() throws Exception { 47 MockitoSession session = mockitoSession().mockStatic(SuperClass.class).startMocking(); 48 try { 49 assertNull(SuperClass.returnD()); 50 51 for (int i = 0; i < 1000; i++) { 52 when(SuperClass.returnD()).thenReturn("fakeD"); 53 assertEquals("fakeD", SuperClass.returnD()); 54 55 reset(staticMockMarker(SuperClass.class)); 56 assertNull(SuperClass.returnD()); 57 58 if (i % 100 == 0) { 59 Log.i(LOG_TAG, "Ran " + i + " tests"); 60 } 61 } 62 } finally { 63 session.finishMocking(); 64 } 65 } 66 67 @Test stressStaticMethod()68 public void stressStaticMethod() throws Exception { 69 MockitoSession session = mockitoSession().mockStatic(SuperClass.class).startMocking(); 70 try { 71 assertNull(SuperClass.returnB()); 72 73 for (int i = 0; i < 10; i++) { 74 when(SuperClass.returnB()).thenReturn("fakeB"); 75 assertEquals("fakeB", SuperClass.returnB()); 76 77 reset(staticMockMarker(SuperClass.class)); 78 assertNull(SuperClass.returnB()); 79 80 Log.i(LOG_TAG, "Ran " + i + " tests"); 81 } 82 } finally { 83 session.finishMocking(); 84 } 85 } 86 87 } 88