BAEL-183 JUnit vs TestNG
This commit is contained in:
parent
ffd17c1b21
commit
1bf70b8f08
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class DependentTests {
|
||||||
|
|
||||||
|
private EmailValidator emailValidator;
|
||||||
|
private LoginValidator loginValidator;
|
||||||
|
private String validEmail = "abc@qwe.com";
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public void setup(){
|
||||||
|
emailValidator = new EmailValidator();
|
||||||
|
loginValidator = new LoginValidator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validEmailTest() {
|
||||||
|
boolean valid = emailValidator.validate(validEmail);
|
||||||
|
Assert.assertEquals(valid, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(dependsOnMethods={"validEmailTest"})
|
||||||
|
public void validateLogin() {
|
||||||
|
boolean valid = loginValidator.validate();
|
||||||
|
Assert.assertEquals(valid, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class EmailValidator{
|
||||||
|
|
||||||
|
public boolean validate(String validEmail) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoginValidator{
|
||||||
|
|
||||||
|
public boolean validate() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Parameterized;
|
||||||
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
|
@RunWith(value = Parameterized.class)
|
||||||
|
public class MyParameterisedUnitTest {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private NameCheck nameCheck;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void initialSetup() {
|
||||||
|
nameCheck = new NameCheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MyParameterisedUnitTest(String myName) {
|
||||||
|
this.name = myName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Parameters
|
||||||
|
public static Collection<Object[]> data() {
|
||||||
|
Object[][] data
|
||||||
|
= new Object[][] { { "Peter" }, { "Sam" }, { "Tim" }, { "Lucy" } };
|
||||||
|
return Arrays.asList(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void pushNameTest() {
|
||||||
|
boolean valid = nameCheck.nameCheck(name);
|
||||||
|
Assert.assertEquals(valid, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NameCheck{
|
||||||
|
|
||||||
|
public boolean nameCheck(String name) {
|
||||||
|
if(name.length()>0)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.DataProvider;
|
||||||
|
import org.testng.annotations.Parameters;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class MyParameterisedUnitTestNg {
|
||||||
|
|
||||||
|
private PrimeNumberCheck primeNumberChecker;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public void intialSetup(){
|
||||||
|
primeNumberChecker = new PrimeNumberCheck();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(enabled=false)
|
||||||
|
@Parameters({"num","expectedResult"})
|
||||||
|
public void parameterCheckTest(int number,boolean expectedResult) {
|
||||||
|
Assert.assertEquals(expectedResult,
|
||||||
|
primeNumberChecker.validate(number));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DataProvider(name = "test1")
|
||||||
|
public static Object[][] primeNumbers() {
|
||||||
|
return new Object[][] {
|
||||||
|
{2, true}, {6, false}, {19, true}, {22, false}, {23, true}};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(dataProvider = "test1")
|
||||||
|
public void testPrimeNumberChecker(Integer inputNumber, Boolean expectedResult) {
|
||||||
|
Assert.assertEquals(expectedResult,
|
||||||
|
primeNumberChecker.validate(inputNumber));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(dataProvider = "myDataProvider")
|
||||||
|
public void parameterCheckTest(User user) {
|
||||||
|
Assert.assertEquals("sam",user.getName());
|
||||||
|
Assert.assertEquals(12,user.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
@DataProvider(name = "myDataProvider")
|
||||||
|
public Object[][] parameterProvider() {
|
||||||
|
User usr = new User();
|
||||||
|
usr.setName("sam");
|
||||||
|
usr.setAge(12);
|
||||||
|
return new Object[][]{{usr}};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class PrimeNumberCheck{
|
||||||
|
|
||||||
|
public Object validate(int number) {
|
||||||
|
for(int i=2;i<number;i++) {
|
||||||
|
if(number%i==0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class User{
|
||||||
|
private String name;
|
||||||
|
private int age;
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MyTest1 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void suiteTest1() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class MyTest2 {
|
||||||
|
@Test
|
||||||
|
public void suiteTest2() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.Suite;
|
||||||
|
|
||||||
|
@RunWith(Suite.class)
|
||||||
|
@Suite.SuiteClasses({
|
||||||
|
MyTest1.class,
|
||||||
|
MyTest2.class
|
||||||
|
})
|
||||||
|
public class MyTest5 {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class RegistrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmail_ifValid_thenCorrect(){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class SignInTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsername_ifValid_thenCorrect(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class SummationServiceTest {
|
||||||
|
private static List<Integer> numbers;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void initialize() {
|
||||||
|
numbers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void tearDown() {
|
||||||
|
numbers = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void runBeforeEachTest() {
|
||||||
|
numbers.add(1);
|
||||||
|
numbers.add(2);
|
||||||
|
numbers.add(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void runAfterEachTest() {
|
||||||
|
numbers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNumbers_sumEquals_thenCorrect() {
|
||||||
|
int sum = 0;
|
||||||
|
for (int num : numbers)
|
||||||
|
sum += num;
|
||||||
|
Assert.assertEquals(6, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Ignore
|
||||||
|
@Test
|
||||||
|
public void givenEmptyList_sumEqualsZero_thenCorrect(){
|
||||||
|
int sum = 0;
|
||||||
|
for (int num : numbers)
|
||||||
|
sum += num;
|
||||||
|
Assert.assertEquals(6, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ArithmeticException.class)
|
||||||
|
public void calculateWithException() {
|
||||||
|
int i = 1/0;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,101 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.testng.Assert;
|
||||||
|
import org.testng.TestNG;
|
||||||
|
import org.testng.annotations.AfterClass;
|
||||||
|
import org.testng.annotations.AfterGroups;
|
||||||
|
import org.testng.annotations.AfterMethod;
|
||||||
|
import org.testng.annotations.BeforeClass;
|
||||||
|
import org.testng.annotations.BeforeGroups;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class SummationServiceTestTestNg extends TestNG{
|
||||||
|
|
||||||
|
private List<Integer> numbers;
|
||||||
|
|
||||||
|
private int testCount=0;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public void initialize() {
|
||||||
|
numbers = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public void tearDown() {
|
||||||
|
numbers = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void runBeforeEachTest() {
|
||||||
|
testCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterMethod
|
||||||
|
public void runAfterEachTest() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeGroups("negative_tests")
|
||||||
|
public void runBeforeEachNegativeGroup() {
|
||||||
|
numbers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeGroups("regression")
|
||||||
|
public void runBeforeEachRegressionGroup() {
|
||||||
|
numbers.add(-11);
|
||||||
|
numbers.add(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeGroups("positive_tests")
|
||||||
|
public void runBeforeEachPositiveGroup() {
|
||||||
|
numbers.add(1);
|
||||||
|
numbers.add(2);
|
||||||
|
numbers.add(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterGroups("positive_tests,regression,negative_tests")
|
||||||
|
public void runAfterEachGroup() {
|
||||||
|
numbers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(groups="positive_tests",enabled=false)
|
||||||
|
public void givenNumbers_sumEquals_thenCorrect() {
|
||||||
|
int sum = 0;
|
||||||
|
for (int num : numbers)
|
||||||
|
sum += num;
|
||||||
|
Assert.assertEquals(sum, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(groups="negative_tests")
|
||||||
|
public void givenEmptyList_sumEqualsZero_thenCorrect(){
|
||||||
|
int sum = 0;
|
||||||
|
for (int num : numbers)
|
||||||
|
sum += num;
|
||||||
|
Assert.assertEquals(0, sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(groups = "regression")
|
||||||
|
public void givenNegativeNumber_sumLessthanZero_thenCorrect() {
|
||||||
|
int sum = 0;
|
||||||
|
for (int num : numbers)
|
||||||
|
sum += num;
|
||||||
|
System.out.println(sum);
|
||||||
|
Assert.assertTrue(sum<0);;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(groups="sanity")
|
||||||
|
public void givenNumbers_doSum(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = ArithmeticException.class)
|
||||||
|
public void calculateWithException() {
|
||||||
|
int i = 1/0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.test.comparison;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class TimeOutTest {
|
||||||
|
@Test(timeOut = 1000,enabled=false)
|
||||||
|
public void testInfinity() {
|
||||||
|
while (true);
|
||||||
|
}
|
||||||
|
}
|
10
core-java/src/test/resources/parameterised_test.xml
Normal file
10
core-java/src/test/resources/parameterised_test.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||||
|
<suite name="My test suite">
|
||||||
|
<test name="testing">
|
||||||
|
<parameter name="num" value="2"/>
|
||||||
|
<parameter name="expectedResult" value="true"/>
|
||||||
|
<classes>
|
||||||
|
<class name="com.baeldung.test.comparison.MyParameterisedUnitTestNg"/>
|
||||||
|
</classes>
|
||||||
|
</test>
|
||||||
|
</suite>
|
13
core-java/src/test/resources/test_group.xml
Normal file
13
core-java/src/test/resources/test_group.xml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||||
|
<suite name="regression_test">
|
||||||
|
<test name="testing">
|
||||||
|
<groups>
|
||||||
|
<run>
|
||||||
|
<include name="sanity" />
|
||||||
|
</run>
|
||||||
|
</groups>
|
||||||
|
<classes>
|
||||||
|
<class name="com.baeldung.test.comparison.SummationServiceTestTestNg" />
|
||||||
|
</classes>
|
||||||
|
</test>
|
||||||
|
</suite>
|
9
core-java/src/test/resources/test_suite.xml
Normal file
9
core-java/src/test/resources/test_suite.xml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||||
|
<suite name="Evaluation_Suite">
|
||||||
|
<test name="Sanity">
|
||||||
|
<classes>
|
||||||
|
<class name="com.baeldung.test.comparison.RegistrationTest" />
|
||||||
|
<class name="com.baeldung.test.comparison.SignInTest" />
|
||||||
|
</classes>
|
||||||
|
</test>
|
||||||
|
</suite>
|
Loading…
x
Reference in New Issue
Block a user