BAEL-639: Fixing failing test

This commit is contained in:
slavisa-baeldung 2017-02-18 07:31:10 +01:00
parent 5f0f26e374
commit 6c282a742c
21 changed files with 265 additions and 124 deletions

View File

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

View File

@ -1,7 +1,4 @@
package com.baeldung.test.comparison; package com.baeldung.junit4vstestng;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -10,16 +7,13 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters; import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;
@RunWith(value = Parameterized.class) @RunWith(value = Parameterized.class)
public class MyParameterisedUnitTest { public class MyParameterisedUnitTest {
private String name; private String name;
private NameCheck nameCheck;
@Before
public void initialSetup() {
nameCheck = new NameCheck();
}
public MyParameterisedUnitTest(String myName) { public MyParameterisedUnitTest(String myName) {
this.name = myName; this.name = myName;
@ -27,23 +21,13 @@ public class MyParameterisedUnitTest {
@Parameters @Parameters
public static Collection<Object[]> data() { public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { "Peter" }, { "Sam" }, { "Tim" }, { "Lucy" } }; Object[][] data = new Object[][]{{"Peter"}, {"Sam"}, {"Tim"}, {"Lucy"}};
return Arrays.asList(data); return Arrays.asList(data);
} }
@Test @Test
public void givenName_whenValidLength_thenTrue() { public void givenName_whenValidLength_thenTrue() {
boolean valid = nameCheck.nameCheck(name); boolean valid = name.length() > 0;
Assert.assertEquals(valid, true); Assert.assertEquals(valid, true);
} }
} }
class NameCheck {
public boolean nameCheck(String name) {
if (name.length() > 0)
return true;
return false;
}
}

View File

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

View File

@ -1,4 +1,4 @@
package com.baeldung.test.comparison; package com.baeldung.junit4vstestng;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;

View File

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

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

@ -194,6 +194,7 @@
<module>struts2</module> <module>struts2</module>
<module>apache-velocity</module> <module>apache-velocity</module>
<module>testng</module>
</modules> </modules>

109
testng/pom.xml Normal file
View File

@ -0,0 +1,109 @@
<?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>
<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

@ -1,4 +1,4 @@
package com.baeldung.test.comparison; package baeldung.com;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -6,9 +6,9 @@ import org.testng.annotations.Test;
public class DependentTests { public class DependentTests {
private String validEmail = "abc@qwe.com";
private EmailValidator emailValidator; private EmailValidator emailValidator;
private LoginValidator loginValidator; private LoginValidator loginValidator;
private String validEmail = "abc@qwe.com";
@BeforeClass @BeforeClass
public void setup() { public void setup() {
@ -23,7 +23,7 @@ public class DependentTests {
} }
@Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" }) @Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" })
public void givenValidEmail_whenLoggedin_thenTrue() { public void givenValidEmail_whenLoggedIn_thenTrue() {
boolean valid = loginValidator.validate(); boolean valid = loginValidator.validate();
Assert.assertEquals(valid, true); Assert.assertEquals(valid, true);
} }
@ -31,7 +31,7 @@ public class DependentTests {
class EmailValidator { class EmailValidator {
public boolean validate(String validEmail) { boolean validate(String validEmail) {
return true; return true;
} }
@ -39,7 +39,7 @@ class EmailValidator {
class LoginValidator { class LoginValidator {
public boolean validate() { boolean validate() {
return true; return true;
} }

View File

@ -1,4 +1,4 @@
package com.baeldung.test.comparison; package baeldung.com;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -6,29 +6,22 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters; import org.testng.annotations.Parameters;
import org.testng.annotations.Test; import org.testng.annotations.Test;
public class MyParameterisedUnitTestNg { public class ParametrizedTestNGTest {
private PrimeNumberCheck primeNumberChecker; @Test
@Parameters({ "name", "expectedResult" })
@BeforeClass public void givenNumber_ifPrime_thenCorrect(String name, boolean expectedResult) {
public void intialSetup() { Assert.assertEquals(expectedResult, name.length() > 0);
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") @DataProvider(name = "test1")
public static Object[][] primeNumbers() { 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") @Test(dataProvider = "test1")
public void givenNumber_whenPrime_thenCorrect(Integer inputNumber, Boolean expectedResult) { public void givenNumber_whenPrime_thenCorrect(String name, boolean expectedResult) {
Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber)); Assert.assertEquals(expectedResult, name.length() > 0);
} }
@Test(dataProvider = "myDataProvider") @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 { class User {
private String name; private String name;

View File

@ -1,4 +1,4 @@
package com.baeldung.test.comparison; package baeldung.com;
import org.testng.annotations.Test; import org.testng.annotations.Test;

View File

@ -1,4 +1,4 @@
package com.baeldung.test.comparison; package baeldung.com;
import org.testng.annotations.Test; import org.testng.annotations.Test;

View File

@ -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.ArrayList;
import java.util.List; import java.util.List;
import org.testng.Assert; public class SummationServiceTest extends TestNG {
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 List<Integer> numbers;
@ -29,14 +23,16 @@ public class SummationServiceTestTestNg extends TestNG {
numbers = null; numbers = null;
} }
@BeforeMethod @BeforeSuite(groups = "regression")
public void runBeforeEachTest() { public void runBeforeRegressionSuite() {
testCount++; numbers = new ArrayList<>();
numbers.add(-11);
numbers.add(2);
} }
@AfterMethod @AfterSuite(groups = "regression")
public void runAfterEachTest() { public void runAfterRegressionSuite() {
numbers = null;
} }
@BeforeGroups("negative_tests") @BeforeGroups("negative_tests")
@ -62,6 +58,17 @@ public class SummationServiceTestTestNg extends TestNG {
numbers.clear(); numbers.clear();
} }
@BeforeMethod
public void runBeforeEachTest() {
testCount++;
}
@AfterMethod
public void runAfterEachTest() {
}
@Test(groups = "positive_tests", enabled = false) @Test(groups = "positive_tests", enabled = false)
public void givenNumbers_sumEquals_thenCorrect() { public void givenNumbers_sumEquals_thenCorrect() {
int sum = numbers.stream().reduce(0, Integer::sum); int sum = numbers.stream().reduce(0, Integer::sum);
@ -78,12 +85,6 @@ public class SummationServiceTestTestNg extends TestNG {
public void givenNegativeNumber_sumLessthanZero_thenCorrect() { public void givenNegativeNumber_sumLessthanZero_thenCorrect() {
int sum = numbers.stream().reduce(0, Integer::sum); int sum = numbers.stream().reduce(0, Integer::sum);
Assert.assertTrue(sum < 0); Assert.assertTrue(sum < 0);
;
}
@Test(groups = "sanity")
public void givenNumbers_doSum() {
} }
@Test(expectedExceptions = ArithmeticException.class) @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; import org.testng.annotations.Test;
public class TimeOutTest { public class TimeOutTest {
@Test(timeOut = 1000, enabled = false) @Test(timeOut = 1000, enabled = false)
public void givenExecution_takeMoreTime_thenFail() { 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,17 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="My test suite">
<test name="test1">
<parameter name="name" value="Peter"/>
<parameter name="expectedResult" value="true"/>
<classes>
<class name="baeldung.com.ParametrizedTestNGTest"/>
</classes>
</test>
<test name="test2">
<parameter name="name" value="Sam"/>
<parameter name="expectedResult" value="true"/>
<classes>
<class name="baeldung.com.ParametrizedTestNGTest"/>
</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="testing">
<groups>
<run>
<include name="regression"/>
</run>
</groups>
<classes>
<class name="baeldung.com.SummationServiceTest"/>
</classes>
</test>
</suite>

View File

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