1 /* 2 * Copyright 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.google.android.testing.mocking.test; 17 18 import android.content.Context; 19 20 import com.google.android.testing.mocking.AndroidMock; 21 import com.google.android.testing.mocking.SdkVersion; 22 import com.google.android.testing.mocking.UsesMocks; 23 import com.google.android.testing.mocking.testapp.ClassToMock; 24 25 import junit.framework.TestCase; 26 27 /** 28 * Tests that Android Mock provides correct mocks when running on a Dalvik VM. 29 * 30 * @author swoodward (Stephen Woodward) 31 */ 32 public class MockingTest extends TestCase { 33 /** 34 * Test that an SDK class is mocked correctly, that is to say the mock 35 * comes from the pre-generated set, and it corresponds to the correct 36 * runtime environment 37 */ 38 @UsesMocks(Context.class) 39 public void testFrameworkMock() { 40 Context mockContext = AndroidMock.createMock(Context.class); 41 String packageName = mockContext.getClass().getPackage().getName(); 42 assertEquals(SdkVersion.getCurrentVersion().getPackagePrefix(), 43 packageName.substring(0, packageName.indexOf('.') + 1)); 44 } 45 46 /** 47 * Test that a non-SDK class is mocked correctly 48 */ 49 @UsesMocks(ClassToMock.class) 50 public void testNormalMock() { 51 ClassToMock myMockClass = AndroidMock.createMock(ClassToMock.class); 52 AndroidMock.expect(myMockClass.getString()).andReturn( 53 "I'm the king of the world, king of the -- d'oh!"); 54 AndroidMock.expect(myMockClass.getInt()).andReturn(42); 55 AndroidMock.replay(myMockClass); 56 assertEquals("I'm the king of the world, king of the -- d'oh!", 57 myMockClass.getString()); 58 assertEquals(42, myMockClass.getInt()); 59 AndroidMock.verify(myMockClass); 60 } 61 } 62 63