Merge branch 'testng-junit'

This commit is contained in:
slavisa-baeldung 2017-02-18 22:57:46 +01:00
commit bf057fa8db
29 changed files with 456 additions and 269 deletions

View File

@ -1,4 +1,4 @@
package com.baeldung.test.comparison;
package com.baeldung.junit4vstestng;
import static org.junit.Assert.assertEquals;
@ -15,7 +15,7 @@ public class DivisibilityTest {
}
@Test
public void givenNumber_whenDivisiblebyTwo_thenCorrect() {
public void givenNumber_whenDivisibleByTwo_thenCorrect() {
assertEquals(number % 2, 0);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.junit4vstestng;
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;
import java.util.Arrays;
import java.util.Collection;
@RunWith(value = Parameterized.class)
public class ParametrizedTests {
private int value;
private boolean isEven;
public ParametrizedTests(int value, boolean isEven) {
this.value = value;
this.isEven = isEven;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][]{{1, false}, {2, true}, {4, true}};
return Arrays.asList(data);
}
@Test
public void givenParametrizedNumber_ifEvenCheckOK_thenCorrect() {
Assert.assertEquals(isEven, value % 2 == 0);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.junit4vstestng;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RegistrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class);
@Test
public void whenCalledFromSuite_thanOK() {
LOGGER.info("Registration successful");
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.junit4vstestng;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SignInTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class);
@Test
public void whenCalledFromSuite_thanOK() {
LOGGER.info("SignIn successful");
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.test.comparison;
package com.baeldung.junit4vstestng;
import static org.junit.Assert.assertEquals;

View File

@ -1,10 +1,10 @@
package com.baeldung.test.comparison;
package com.baeldung.junit4vstestng;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({ StringCaseTest.class, DivisibilityTest.class })
@Suite.SuiteClasses({ RegistrationTest.class, SignInTest.class })
public class SuiteTest {
}

View File

@ -1,8 +1,4 @@
package com.baeldung.test.comparison;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
package com.baeldung.junit4vstestng;
import org.junit.After;
import org.junit.AfterClass;
@ -12,6 +8,9 @@ import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class SummationServiceTest {
private static List<Integer> numbers;

View File

@ -1,46 +0,0 @@
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 givenEmail_ifValid_thenTrue() {
boolean valid = emailValidator.validate(validEmail);
Assert.assertEquals(valid, true);
}
@Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" })
public void givenValidEmail_whenLoggedin_thenTrue() {
boolean valid = loginValidator.validate();
Assert.assertEquals(valid, true);
}
}
class EmailValidator {
public boolean validate(String validEmail) {
return true;
}
}
class LoginValidator {
public boolean validate() {
return true;
}
}

View File

@ -1,49 +0,0 @@
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 givenName_whenValidLength_thenTrue() {
boolean valid = nameCheck.nameCheck(name);
Assert.assertEquals(valid, true);
}
}
class NameCheck {
public boolean nameCheck(String name) {
if (name.length() > 0)
return true;
return false;
}
}

View File

@ -1,81 +0,0 @@
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 givenNumber_ifPrime_thenCorrect(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 givenNumber_whenPrime_thenCorrect(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;
}
}

View File

@ -1,11 +0,0 @@
package com.baeldung.test.comparison;
import org.testng.annotations.Test;
public class RegistrationTest {
@Test
public void givenEmail_ifValid_thenCorrect() {
}
}

View File

@ -1,12 +0,0 @@
package com.baeldung.test.comparison;
import org.testng.annotations.Test;
public class SignInTest {
@Test
public void givenUsername_ifValid_thenCorrect() {
}
}

View File

@ -1,10 +0,0 @@
<!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>

View File

@ -1,13 +0,0 @@
<!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>

View File

@ -1,9 +0,0 @@
<!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>

View File

@ -182,6 +182,7 @@
<module>spring-reactor</module>
<module>testing</module>
<module>testng</module>
<module>video-tutorials</module>
@ -194,7 +195,6 @@
<module>struts2</module>
<module>apache-velocity</module>
</modules>
<build>

115
testng/pom.xml Normal file
View File

@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>testng</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testng</name>
<dependencies>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>testng</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
</excludes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version>
<logback.version>1.1.7</logback.version>
<!-- testing -->
<testng.version>6.10</testng.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,25 @@
package baeldung.com;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;
public class DependentTests {
private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class);
private String email = "abc@qwe.com";
@Test
public void givenEmail_ifValid_thenTrue() {
boolean valid = email.contains("@");
Assert.assertEquals(valid, true);
}
@Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"})
public void givenValidEmail_whenLoggedIn_thenTrue() {
LOGGER.info("Email {} valid >> logging in", email);
}
}

View File

@ -0,0 +1,77 @@
package baeldung.com;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParametrizedTests {
private static final Logger LOGGER = LoggerFactory.getLogger(ParametrizedTests.class);
@Test
@Parameters({"value", "isEven"})
public void givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) {
Assert.assertEquals(isEven, value % 2 == 0);
}
@DataProvider(name = "numbers")
public static Object[][] evenNumbers() {
return new Object[][]{{1, false}, {2, true}, {4, true}};
}
@Test(dataProvider = "numbers")
public void givenNumberFromDataProvider_ifEvenCheckOK_thenCorrect(Integer number, boolean expected) {
Assert.assertEquals(expected, number % 2 == 0);
}
@Test(dataProvider = "numbersObject")
public void givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect(EvenNumber number) {
Assert.assertEquals(number.isEven(), number.getValue() % 2 == 0);
}
@DataProvider(name = "numbersObject")
public Object[][] parameterProvider() {
return new Object[][]{{new EvenNumber(1, false)}, {new EvenNumber(2, true)}, {new EvenNumber(4, true),}};
}
}
class EvenNumber {
private int value;
private boolean isEven;
public EvenNumber(int number, boolean isEven) {
this.value = number;
this.isEven = isEven;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public boolean isEven() {
return isEven;
}
public void setEven(boolean even) {
isEven = even;
}
@Override
public String toString() {
return "EvenNumber{" +
"value=" + value +
", isEven=" + isEven +
'}';
}
}

View File

@ -0,0 +1,14 @@
package baeldung.com;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
public class RegistrationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class);
@Test
public void whenCalledFromSuite_thanOK() {
LOGGER.info("Registration successful");
}
}

View File

@ -0,0 +1,14 @@
package baeldung.com;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
public class SignInTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class);
@Test
public void whenCalledFromSuite_thanOK() {
LOGGER.info("SignIn successful");
}
}

View File

@ -1,19 +1,16 @@
package com.baeldung.test.comparison;
package baeldung.com;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.*;
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 {
public class SummationServiceTest extends TestNG {
private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class);
private List<Integer> numbers;
@ -29,14 +26,16 @@ public class SummationServiceTestTestNg extends TestNG {
numbers = null;
}
@BeforeMethod
public void runBeforeEachTest() {
testCount++;
@BeforeSuite(groups = "regression")
public void runBeforeRegressionSuite() {
numbers = new ArrayList<>();
numbers.add(-11);
numbers.add(2);
}
@AfterMethod
public void runAfterEachTest() {
@AfterSuite(groups = "regression")
public void runAfterRegressionSuite() {
numbers = null;
}
@BeforeGroups("negative_tests")
@ -62,6 +61,17 @@ public class SummationServiceTestTestNg extends TestNG {
numbers.clear();
}
@BeforeMethod
public void runBeforeEachTest() {
testCount++;
}
@AfterMethod
public void runAfterEachTest() {
}
@Test(groups = "positive_tests", enabled = false)
public void givenNumbers_sumEquals_thenCorrect() {
int sum = numbers.stream().reduce(0, Integer::sum);
@ -78,12 +88,6 @@ public class SummationServiceTestTestNg extends TestNG {
public void givenNegativeNumber_sumLessthanZero_thenCorrect() {
int sum = numbers.stream().reduce(0, Integer::sum);
Assert.assertTrue(sum < 0);
;
}
@Test(groups = "sanity")
public void givenNumbers_doSum() {
}
@Test(expectedExceptions = ArithmeticException.class)

View File

@ -0,0 +1,44 @@
package baeldung.com;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
public class TestGroup {
@BeforeGroups("database")
public void setupDB() {
System.out.println("setupDB()");
}
@AfterGroups("database")
public void cleanDB() {
System.out.println("cleanDB()");
}
@Test(groups= "selenium-test")
public void runSelenium() {
System.out.println("runSelenium()");
}
@Test(groups= "selenium-test")
public void runSelenium1() {
System.out.println("runSelenium()1");
}
@Test(groups = "database")
public void testConnectOracle() {
System.out.println("testConnectOracle()");
}
@Test(groups = "database")
public void testConnectMsSQL() {
System.out.println("testConnectMsSQL");
}
@Test(dependsOnGroups = {"database","selenium-test"})
public void runFinal() {
System.out.println("runFinal");
}
}

View File

@ -1,11 +1,11 @@
package com.baeldung.test.comparison;
package baeldung.com;
import org.testng.annotations.Test;
public class TimeOutTest {
@Test(timeOut = 1000, enabled = false)
public void givenExecution_takeMoreTime_thenFail() {
while (true)
;
while (true) ;
}
}

View File

@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,10 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="My test suite">
<test name="numbersXML">
<parameter name="value" value="1"/>
<parameter name="isEven" value="false"/>
<classes>
<class name="baeldung.com.ParametrizedTests"/>
</classes>
</test>
</suite>

View File

@ -0,0 +1,13 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="regression_test">
<test name="test groups">
<groups>
<run>
<include name="regression"/>
</run>
</groups>
<classes>
<class name="baeldung.com.SummationServiceTest"/>
</classes>
</test>
</suite>

View File

@ -0,0 +1,17 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="regression_test">
<test name="test setup">
<groups>
<run>
<include name="regression"/>
</run>
</groups>
<classes>
<class name="baeldung.com.SummationServiceTest">
<methods>
<include name="givenNumbers_sumEquals_thenCorrect"/>
</methods>
</class>
</classes>
</test>
</suite>

View File

@ -0,0 +1,9 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="suite">
<test name="test suite">
<classes>
<class name="baeldung.com.RegistrationTest" />
<class name="baeldung.com.SignInTest" />
</classes>
</test>
</suite>