1 /* 2 * Copyright (C) 2012 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.test.runner; 17 18 import android.content.Context; 19 import android.os.Bundle; 20 21 import org.junit.Before; 22 import org.junit.Test; 23 import org.mockito.Mock; 24 import org.mockito.Mockito; 25 import org.mockito.MockitoAnnotations; 26 27 28 import java.io.ByteArrayOutputStream; 29 import java.io.PrintStream; 30 31 /** 32 * Unit tests for {@link AndroidJUnitRunner}. 33 */ 34 public class AndroidJUnitRunnerTest { 35 36 private AndroidJUnitRunner mAndroidJUnitRunner; 37 private PrintStream mStubStream; 38 @Mock 39 private TestRequestBuilder mMockBuilder; 40 @Mock 41 private Context mMockContext; 42 43 @Before setUp()44 public void setUp() throws Exception { 45 mAndroidJUnitRunner = new AndroidJUnitRunner() { 46 47 48 @Override 49 TestRequestBuilder createTestRequestBuilder(PrintStream writer, 50 String... packageCodePaths) { 51 return mMockBuilder; 52 } 53 54 @Override 55 public Context getContext() { 56 return mMockContext; 57 } 58 }; 59 mAndroidJUnitRunner.setArguments(new Bundle()); 60 mStubStream = new PrintStream(new ByteArrayOutputStream()); 61 MockitoAnnotations.initMocks(this); 62 } 63 64 /** 65 * Test {@link AndroidJUnitRunner#buildRequest(Bundle, PrintStream)} when 66 * a single class name is provided. 67 */ 68 @Test testBuildRequest_singleClass()69 public void testBuildRequest_singleClass() { 70 Bundle b = new Bundle(); 71 b.putString(AndroidJUnitRunner.ARGUMENT_TEST_CLASS, "ClassName"); 72 mAndroidJUnitRunner.buildRequest(b, mStubStream); 73 Mockito.verify(mMockBuilder).addTestClass("ClassName"); 74 } 75 76 /** 77 * Test {@link AndroidJUnitRunner#buildRequest(Bundle, PrintStream)} when 78 * multiple class names are provided. 79 */ 80 @Test testBuildRequest_multiClass()81 public void testBuildRequest_multiClass() { 82 Bundle b = new Bundle(); 83 b.putString(AndroidJUnitRunner.ARGUMENT_TEST_CLASS, "ClassName1,ClassName2"); 84 mAndroidJUnitRunner.buildRequest(b, mStubStream); 85 Mockito.verify(mMockBuilder).addTestClass("ClassName1"); 86 Mockito.verify(mMockBuilder).addTestClass("ClassName2"); 87 } 88 89 /** 90 * Test {@link AndroidJUnitRunner#buildRequest(Bundle, PrintStream)} when 91 * class name and method name is provided. 92 */ 93 @Test testBuildRequest_method()94 public void testBuildRequest_method() { 95 Bundle b = new Bundle(); 96 b.putString(AndroidJUnitRunner.ARGUMENT_TEST_CLASS, "ClassName1#method"); 97 mAndroidJUnitRunner.buildRequest(b, mStubStream); 98 Mockito.verify(mMockBuilder).addTestMethod("ClassName1", "method"); 99 } 100 } 101