1 /* 2 * Copyright (C) 2016 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.cts.core.runner.support; 18 19 import java.lang.reflect.Method; 20 21 import org.junit.runner.Runner; 22 import org.junit.runners.model.RunnerBuilder; 23 24 import org.testng.annotations.Test; 25 26 /** 27 * A {@link RunnerBuilder} that can handle TestNG tests. 28 */ 29 public class TestNgRunnerBuilder extends RunnerBuilder { 30 // Returns a TestNG runner for this class, only if it is a class 31 // annotated with testng's @Test or has any methods with @Test in it. 32 @Override runnerForClass(Class<?> testClass)33 public Runner runnerForClass(Class<?> testClass) { 34 if (isTestNgTestClass(testClass)) { 35 return new TestNgRunner(testClass); 36 } 37 38 return null; 39 } 40 isTestNgTestClass(Class<?> cls)41 private static boolean isTestNgTestClass(Class<?> cls) { 42 // TestNG test is either marked @Test at the class 43 if (cls.getAnnotation(Test.class) != null) { 44 return true; 45 } 46 47 // Or It's marked @Test at the method level 48 for (Method m : cls.getDeclaredMethods()) { 49 if (m.getAnnotation(Test.class) != null) { 50 return true; 51 } 52 } 53 54 return false; 55 } 56 } 57