diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java b/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java index 6b0394a368..3ef4949067 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java +++ b/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java @@ -5,42 +5,42 @@ 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); - } + 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{ +class EmailValidator { + + public boolean validate(String validEmail) { + return true; + } - public boolean validate(String validEmail) { - return true; - } - } -class LoginValidator{ +class LoginValidator { + + public boolean validate() { + return true; + } - public boolean validate() { - return true; - } - } diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java b/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java new file mode 100644 index 0000000000..9ae13f5934 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java @@ -0,0 +1,21 @@ +package com.baeldung.test.comparison; + +import static org.junit.Assert.assertEquals; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class DivisibilityTest { + + private static int number; + + @BeforeClass + public static void setup() { + number = 40; + } + + @Test + 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/test/comparison/MyParameterisedUnitTest.java index 9e63956556..3372bbb577 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java +++ b/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java @@ -12,39 +12,38 @@ 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 data() { - Object[][] data - = new Object[][] { { "Peter" }, { "Sam" }, { "Tim" }, { "Lucy" } }; + Object[][] data = new Object[][] { { "Peter" }, { "Sam" }, { "Tim" }, { "Lucy" } }; return Arrays.asList(data); } - + @Test - public void pushNameTest() { + public void givenName_whenValidLength_thenTrue() { boolean valid = nameCheck.nameCheck(name); Assert.assertEquals(valid, true); } } -class NameCheck{ +class NameCheck { + + public boolean nameCheck(String name) { + if (name.length() > 0) + return true; + return false; + } - 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/MyParameterisedUnitTestNg.java b/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java index d5e4465b3d..4096c3fb6f 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java +++ b/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java @@ -7,74 +7,75 @@ 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}}; - } + + 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{ +class PrimeNumberCheck { + + public Object validate(int number) { + for (int i = 2; i < number; i++) { + if (number % i == 0) + return false; + } + return true; + } - public Object validate(int number) { - for(int i=2;i numbers; - + private static List 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; + int sum = numbers.stream() + .reduce(0, Integer::sum); 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; + public void givenEmptyList_sumEqualsZero_thenCorrect() { + int sum = numbers.stream() + .reduce(0, Integer::sum); + Assert.assertEquals(6, sum); + } + + @Test(expected = ArithmeticException.class) + public void givenNumber_whenThrowsException_thenCorrect() { + int i = 1 / 0; } } diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java b/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java index aecc790374..fb02f28d06 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java +++ b/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java @@ -13,89 +13,82 @@ import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -public class SummationServiceTestTestNg extends TestNG{ - - private List numbers; - - private int testCount=0; - - @BeforeClass - public void initialize() { - numbers = new ArrayList<>(); - } +public class SummationServiceTestTestNg extends TestNG { - @AfterClass - public void tearDown() { - numbers = null; - } + private List numbers; - @BeforeMethod - public void runBeforeEachTest() { - testCount++; - } + private int testCount = 0; - @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) + @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; + int sum = numbers.stream().reduce(0, Integer::sum); 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; - } - - + + @Test(groups = "negative_tests") + public void givenEmptyList_sumEqualsZero_thenCorrect() { + int sum = numbers.stream().reduce(0, Integer::sum); + Assert.assertEquals(0, sum); + } + + @Test(groups = "regression") + 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) + public void givenNumber_whenThrowsException_thenCorrect() { + int i = 1 / 0; + } + } diff --git a/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java b/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java index baf2db9521..6e06132009 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java +++ b/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java @@ -3,8 +3,9 @@ package com.baeldung.test.comparison; import org.testng.annotations.Test; public class TimeOutTest { - @Test(timeOut = 1000,enabled=false) - public void testInfinity() { - while (true); - } + @Test(timeOut = 1000, enabled = false) + public void givenExecution_takeMoreTime_thenFail() { + while (true) + ; + } } diff --git a/core-java/src/test/java/temp/SummationServiceTest.java b/core-java/src/test/java/temp/SummationServiceTest.java new file mode 100644 index 0000000000..3c2d39f5dd --- /dev/null +++ b/core-java/src/test/java/temp/SummationServiceTest.java @@ -0,0 +1,49 @@ +package temp; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import junit.framework.Assert; + +public class SummationServiceTest { + + private static List 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; + assertEquals(6, sum); + } +} \ No newline at end of file