title: Conditional Execution of Test Cases in TestNG
body: <p>I'm using appium to perform tests on mobile applications. What I'm trying to do is only execute the test cases written in Class 1 if the value of "numberOfAssets" is 1. And then ONLY execute test cases written in class 2 if "numberOfAssets" is 2.<br>
  I have been trying to find a fix for this and stumbled upon the "Annotation Transformer" class. But the sad part is that it ends up setting ALL the "@Test" annotations to true. </p>
  
  <p>All i want is for only one whole class to execute based on the "numberOfAssets" value. I would appreciate some suggestions regarding this.</p>
  
  <p>I have three classes-</p>
  
  <p>initialSetUp class-</p>
  
  <pre><code>public class initialSetUp{
      AndroidDriver driver;
      int numberOfAssets = 1;
  @BeforeClass
      public void setUp() throws MalformedURLException {
          //Set up desired capabilities and pass the Android app-activity and app-package to Appium
          DesiredCapabilities capabilities = new DesiredCapabilities();
          capabilities.setCapability("BROWSER_NAME", "chrome");
          capabilities.setCapability("VERSION", "4.4.2"); 
          capabilities.setCapability("deviceName","Emulator");
          capabilities.setCapability("platformName","Android");           
          capabilities.setCapability("appPackage", "com.temp.app_name");
          capabilities.setCapability("appActivity","com.temp.app_name.start_activity"); 
          driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    
      }
  
      @AfterClass
      public void teardown(){
          //close the app
          driver.quit();
      }
  }
  </code></pre>
  
  <p>Class 1-</p>
  
  <pre><code>    @Listeners(AnnotationTransformer.class)
      @Test(enabled = false, groups = "yo")
      public class oneAssetType extends initialSetUp{
      //Check if first page is asset type specified
  //  @Test (groups = "onlyOne", priority = 1)
      public void test_1() throws Exception {
          WebElement centerLable = driver.findElement(By.id("pagerTitle"));
          assert centerLable.getText().equalsIgnoreCase("Whatever") :  "did not match with expected value";
      }
  }
  </code></pre>
  
  <p>Class 2-</p>
  
  <pre><code>@Test(enabled = false)
      public class twoAssetType extends initialSetUp{
          //Check if first page is stickers
          @Test (groups = {"twoAssets", "a"}, priority = 1) 
          public void test_1() throws Exception {
              WebElement centerLable = driver.findElement(By.id("pagerTitle"));
              assert centerLable.getText().equals("WhereEver") : "did not match with expected value";
              }
      }
  </code></pre>
  
  <p>TestNG.xml-</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" &gt;
  
  &lt;suite name="Main App Suite"&gt;
     &lt;listeners&gt;
          &lt;listener class-name="package_name.AnnotationTransformer" /&gt;
      &lt;/listeners&gt;
  
     &lt;test name="Navigate one Asset Types"&gt;
        &lt;classes&gt;
          &lt;class name="package_name.oneAssetType" /&gt;
        &lt;/classes&gt;
     &lt;/test&gt; 
  
   &lt;test name="Navigate two asset types"&gt;
         &lt;classes&gt;
             &lt;class name="package_name.twoAssetType" /&gt;
         &lt;/classes&gt;
     &lt;/test&gt;   
  &lt;/suite&gt;
  </code></pre>
  
  <p>I have also setup a annotation transformer class-</p>
  
  <pre><code>public class AnnotationTransformer implements IAnnotationTransformer {
      initialSetUp setup = new initialSetUp();
      @Override
      public void transform (ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){        
          try{
              if (testClass.getName().equals("package_name.oneAssetType") &amp;&amp; (setup.numberOfAssets == 1)) {
                  annotation.setEnabled(true);
              }
          }catch(NullPointerException e){
              //YO
          }
      }
  </code></pre>