Thursday, June 12, 2014

Basic Usage

package BasicUsage;

import java.util.ArrayList;
import java.util.Collection;

import org.testng.AssertJUnit;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class JunitTest1 {

    private Collection collection;

    @BeforeClass
    public static void oneTimeSetUp() {
        // one-time initialization code  
    System.out.println("@BeforeClass - oneTimeSetUp");
    }

    @AfterClass
    public static void oneTimeTearDown() {
        // one-time cleanup code
    System.out.println("@AfterClass - oneTimeTearDown");
    }

@BeforeMethod

public void setUp() {
        collection = new ArrayList();
        System.out.println("@Before - setUp");
    }

@AfterMethod
public void tearDown() {
        collection.clear();
        System.out.println("@After - tearDown");
    }

    @Test
    public void testEmptyCollection() {
        AssertJUnit.assertTrue(collection.isEmpty());
        System.out.println("@Test - testEmptyCollection");
    }

    @Test
    public void testOneItemCollection() {
        collection.add("itemA");
        AssertJUnit.assertEquals(1, collection.size());
        System.out.println("@Test - testOneItemCollection");
    }
}



package BasicUsage;

import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;

public class JunitTest2 {

@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
int i = 1 / 0;
}

}


package BasicUsage;

import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;



public class JunitTest3 {

@Test(enabled = false)
public void divisionWithException() {
 System.out.println("Method is not ready yet");
}

}

package BasicUsage;

import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;


public class JunitTest4 {

@Test(timeOut = 1000)
public void infinity() {
while (true);
}

}

package BasicUsage;

import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.annotations.Test;

import com.beust.jcommander.Parameterized;

import java.util.Arrays;
import java.util.Collection;


// Paramterized test is done in differnt way in TestNG.So,program is showing error.
@Test
@RunWith(value = Parameterized.class)
public class JunitTest6 {

private int number;

public JunitTest6(int number) {
   this.number = number;
}

@Parameters
public static Collection<Object[]> data() {
  Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
  return Arrays.asList(data);
}

public void pushTest() {
  System.out.println("Parameterized Number is : " + number);
}


}

No comments:

Post a Comment