BAEL-639: Fixing failing test
This commit is contained in:
parent
5f0f26e374
commit
6c282a742c
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<Object[]> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.test.comparison;
|
||||
package com.baeldung.junit4vstestng;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.test.comparison;
|
||||
package com.baeldung.junit4vstestng;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
|
@ -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;
|
||||
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
1
pom.xml
1
pom.xml
|
@ -194,6 +194,7 @@
|
|||
<module>struts2</module>
|
||||
<module>apache-velocity</module>
|
||||
|
||||
<module>testng</module>
|
||||
|
||||
</modules>
|
||||
|
||||
|
|
|
@ -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>
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.test.comparison;
|
||||
package baeldung.com;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.test.comparison;
|
||||
package baeldung.com;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
|
@ -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<Integer> 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)
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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) ;
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
Loading…
Reference in New Issue