2

I have 3 java test classes. 1. ListenerTest1.java 2. ListenerTest2.java 3. ListenerMain.java

Above two testfixture classes i.e ListenerTest1.java and ListenerTest2.java have test methods . Also , having build.gradle file as :-

task automationTests(type: Test) {

    systemProperty 'serviceType', System.properties['serviceType']


    useTestNG {
        useDefaultListeners = true
        suiteXmlBuilder().suite(name: 'Testing case') {
            test(name: 'ServiceUITests', annotations: 'JDK', verbose: '1') {
                classes([:]) {

                    'class'(name: 'company.platform.ListenerMain')

                }
            }
        }
    }
}

So, while running my hudson job , it calls the task i.e automationTests and runs my ListenerMain class. I need to add if/else condition in the ListenerMain class file which will differentiate to run the either of the above two classes mentioned i.e ListenerTest1.java or ListenerTest2.java on the basis of System.property added in hudson job.

E.g

@Listeners(value = {PreconditionListener.class})
public class ListenerMain  {
    private static String serviceTypeUseParameter ;
    private static Logger logger = LogManager.getLogger(ListenerMain .class.getName());
    @BeforeClass
    //TestNg Annotation
    public void setup()
    {
        //#############   Hudson job parameter is serviceType ##########
serviceTypeUseParameter = System.getProperty("serviceType");
        if (serviceTypeUseParameter.equals("ListenerTest1Method")){
        // Run ListenerTest1 class
        }
        else{
        // Run ListenerTest2 class
        }
    }
}

What code need to be added for loading the class as per if/else condition ? I can add main method in ListenerMain class if required.

Little bird
  • 1,106
  • 7
  • 28
  • 58

2 Answers2

1

You can add the below code in if/else code. and within the runClasses method you can specify the test class.

Result result = JUnitCore.runClasses(ListenerTest1.class);
Umais Gillani
  • 608
  • 4
  • 9
0

You have two options in TestNG:

  1. Implement an annotation transformer.
  2. Use BeanShell

Your annotation transformer would test the condition and then override the @Test annotation to add the attribute "enabled=false" if the condition is not satisfied.

Umais Gillani
  • 608
  • 4
  • 9
  • As mentioned , i have 2 diff classes i.e ListenerTest1 and ListenerTest2, So i need to call a whole class from ListenerMain class.I think above links would help if one class consists of different groups methods. – Little bird Nov 02 '16 at 10:51