diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java similarity index 74% rename from core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java index 9ae13f5934..dec7dbe6aa 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java @@ -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); } } diff --git a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java similarity index 59% rename from core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java index 3372bbb577..95f9274af3 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java @@ -1,7 +1,4 @@ -package com.baeldung.test.comparison; - -import java.util.Arrays; -import java.util.Collection; +package com.baeldung.junit4vstestng; import org.junit.Assert; import org.junit.Before; @@ -10,16 +7,13 @@ 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 MyParameterisedUnitTest { private String name; - private NameCheck nameCheck; - - @Before - public void initialSetup() { - nameCheck = new NameCheck(); - } public MyParameterisedUnitTest(String myName) { this.name = myName; @@ -27,23 +21,13 @@ public class MyParameterisedUnitTest { @Parameters public static Collection data() { - Object[][] data = new Object[][] { { "Peter" }, { "Sam" }, { "Tim" }, { "Lucy" } }; + Object[][] data = new Object[][]{{"Peter"}, {"Sam"}, {"Tim"}, {"Lucy"}}; return Arrays.asList(data); } @Test public void givenName_whenValidLength_thenTrue() { - boolean valid = nameCheck.nameCheck(name); + boolean valid = name.length() > 0; Assert.assertEquals(valid, true); } } - -class NameCheck { - - public boolean nameCheck(String name) { - if (name.length() > 0) - return true; - return false; - } - -} diff --git a/core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java similarity index 90% rename from core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java index b4226b82e7..520017e4c1 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package com.baeldung.junit4vstestng; import static org.junit.Assert.assertEquals; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java similarity index 83% rename from core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java index a30e5d312a..3d6d6cbbfb 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package com.baeldung.junit4vstestng; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java similarity index 94% rename from core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java index b76a87c0fe..7d1bf3b7af 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java @@ -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 numbers; diff --git a/core-java/src/test/resources/parameterised_test.xml b/core-java/src/test/resources/parameterised_test.xml deleted file mode 100644 index 69a2c60460..0000000000 --- a/core-java/src/test/resources/parameterised_test.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/core-java/src/test/resources/test_group.xml b/core-java/src/test/resources/test_group.xml deleted file mode 100644 index 0c9a6c73df..0000000000 --- a/core-java/src/test/resources/test_group.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/core-java/src/test/resources/test_suite.xml b/core-java/src/test/resources/test_suite.xml deleted file mode 100644 index 36305aa5fc..0000000000 --- a/core-java/src/test/resources/test_suite.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index ecb07e987b..eb89b9cbaa 100644 --- a/pom.xml +++ b/pom.xml @@ -194,6 +194,7 @@ struts2 apache-velocity + testng diff --git a/testng/pom.xml b/testng/pom.xml new file mode 100644 index 0000000000..02f51d4d99 --- /dev/null +++ b/testng/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + com.baeldung + testng + 0.1.0-SNAPSHOT + jar + testng + + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + org.testng + testng + ${testng.version} + test + + + + + + testng + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + + + + + + + 1.7.21 + 1.1.7 + + + 6.10 + + + 3.6.0 + 2.19.1 + + + + + \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java b/testng/src/test/java/baeldung/com/DependentTests.java similarity index 82% rename from core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java rename to testng/src/test/java/baeldung/com/DependentTests.java index 3ef4949067..ab593e0439 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java +++ b/testng/src/test/java/baeldung/com/DependentTests.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -6,9 +6,9 @@ import org.testng.annotations.Test; public class DependentTests { + private String validEmail = "abc@qwe.com"; private EmailValidator emailValidator; private LoginValidator loginValidator; - private String validEmail = "abc@qwe.com"; @BeforeClass public void setup() { @@ -23,7 +23,7 @@ public class DependentTests { } @Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" }) - public void givenValidEmail_whenLoggedin_thenTrue() { + public void givenValidEmail_whenLoggedIn_thenTrue() { boolean valid = loginValidator.validate(); Assert.assertEquals(valid, true); } @@ -31,7 +31,7 @@ public class DependentTests { class EmailValidator { - public boolean validate(String validEmail) { + boolean validate(String validEmail) { return true; } @@ -39,7 +39,7 @@ class EmailValidator { class LoginValidator { - public boolean validate() { + boolean validate() { return true; } diff --git a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java b/testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java similarity index 51% rename from core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java rename to testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java index 4096c3fb6f..e4f3aacf5b 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java +++ b/testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -6,29 +6,22 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Parameters; import org.testng.annotations.Test; -public class MyParameterisedUnitTestNg { +public class ParametrizedTestNGTest { - 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)); + @Test + @Parameters({ "name", "expectedResult" }) + public void givenNumber_ifPrime_thenCorrect(String name, boolean expectedResult) { + Assert.assertEquals(expectedResult, name.length() > 0); } @DataProvider(name = "test1") public static Object[][] primeNumbers() { - return new Object[][] { { 2, true }, { 6, false }, { 19, true }, { 22, false }, { 23, true } }; + return new Object[][] { { "Peter", true }, { "Sam", true }, { "Tim", true }, { "Lucy", true } }; } @Test(dataProvider = "test1") - public void givenNumber_whenPrime_thenCorrect(Integer inputNumber, Boolean expectedResult) { - Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber)); + public void givenNumber_whenPrime_thenCorrect(String name, boolean expectedResult) { + Assert.assertEquals(expectedResult, name.length() > 0); } @Test(dataProvider = "myDataProvider") @@ -47,17 +40,6 @@ public class MyParameterisedUnitTestNg { } -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; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java b/testng/src/test/java/baeldung/com/RegistrationTest.java similarity index 78% rename from core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java rename to testng/src/test/java/baeldung/com/RegistrationTest.java index 86836425a9..7cb17c9cb8 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java +++ b/testng/src/test/java/baeldung/com/RegistrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.annotations.Test; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java b/testng/src/test/java/baeldung/com/SignInTest.java similarity index 78% rename from core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java rename to testng/src/test/java/baeldung/com/SignInTest.java index 9669f60b6b..4884154345 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java +++ b/testng/src/test/java/baeldung/com/SignInTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.annotations.Test; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java b/testng/src/test/java/baeldung/com/SummationServiceTest.java similarity index 79% rename from core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java rename to testng/src/test/java/baeldung/com/SummationServiceTest.java index fb02f28d06..471d7a7c8a 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java +++ b/testng/src/test/java/baeldung/com/SummationServiceTest.java @@ -1,19 +1,13 @@ -package com.baeldung.test.comparison; +package baeldung.com; + +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 List numbers; @@ -29,14 +23,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 +58,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 +85,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) diff --git a/testng/src/test/java/baeldung/com/TestGroup.java b/testng/src/test/java/baeldung/com/TestGroup.java new file mode 100644 index 0000000000..08bb5c996e --- /dev/null +++ b/testng/src/test/java/baeldung/com/TestGroup.java @@ -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"); + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java b/testng/src/test/java/baeldung/com/TimeOutTest.java similarity index 70% rename from core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java rename to testng/src/test/java/baeldung/com/TimeOutTest.java index 6e06132009..d54a914e08 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java +++ b/testng/src/test/java/baeldung/com/TimeOutTest.java @@ -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) ; } } diff --git a/testng/src/test/resources/logback.xml b/testng/src/test/resources/logback.xml new file mode 100644 index 0000000000..e9ae1894a6 --- /dev/null +++ b/testng/src/test/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/parameterised_test.xml b/testng/src/test/resources/parameterised_test.xml new file mode 100644 index 0000000000..2381ba67d0 --- /dev/null +++ b/testng/src/test/resources/parameterised_test.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_group.xml b/testng/src/test/resources/test_group.xml new file mode 100644 index 0000000000..d636a4174d --- /dev/null +++ b/testng/src/test/resources/test_group.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_suite.xml b/testng/src/test/resources/test_suite.xml new file mode 100644 index 0000000000..201ed3f645 --- /dev/null +++ b/testng/src/test/resources/test_suite.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file