BAEL-183 - refactoring and moving testng to dedicated module
This commit is contained in:
parent
aef624dd8b
commit
333953b204
@ -11,23 +11,24 @@ import java.util.Arrays;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
@RunWith(value = Parameterized.class)
|
@RunWith(value = Parameterized.class)
|
||||||
public class MyParameterisedUnitTest {
|
public class ParametrizedTests {
|
||||||
|
|
||||||
private String name;
|
private int value;
|
||||||
|
private boolean isEven;
|
||||||
|
|
||||||
public MyParameterisedUnitTest(String myName) {
|
public ParametrizedTests(int value, boolean isEven) {
|
||||||
this.name = myName;
|
this.value = value;
|
||||||
|
this.isEven = isEven;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Parameters
|
@Parameters
|
||||||
public static Collection<Object[]> data() {
|
public static Collection<Object[]> data() {
|
||||||
Object[][] data = new Object[][]{{"Peter"}, {"Sam"}, {"Tim"}, {"Lucy"}};
|
Object[][] data = new Object[][]{{1, false}, {2, true}, {4, true}};
|
||||||
return Arrays.asList(data);
|
return Arrays.asList(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenName_whenValidLength_thenTrue() {
|
public void givenParametrizedNumber_ifEvenCheckOK_thenCorrect() {
|
||||||
boolean valid = name.length() > 0;
|
Assert.assertEquals(isEven, value % 2 == 0);
|
||||||
Assert.assertEquals(valid, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -4,7 +4,7 @@ import org.junit.runner.RunWith;
|
|||||||
import org.junit.runners.Suite;
|
import org.junit.runners.Suite;
|
||||||
|
|
||||||
@RunWith(Suite.class)
|
@RunWith(Suite.class)
|
||||||
@Suite.SuiteClasses({ StringCaseTest.class, DivisibilityTest.class })
|
@Suite.SuiteClasses({ RegistrationTest.class, SignInTest.class })
|
||||||
public class SuiteTest {
|
public class SuiteTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
3
pom.xml
3
pom.xml
@ -182,6 +182,7 @@
|
|||||||
<module>spring-reactor</module>
|
<module>spring-reactor</module>
|
||||||
|
|
||||||
<module>testing</module>
|
<module>testing</module>
|
||||||
|
<module>testng</module>
|
||||||
|
|
||||||
<module>video-tutorials</module>
|
<module>video-tutorials</module>
|
||||||
|
|
||||||
@ -194,8 +195,6 @@
|
|||||||
<module>struts2</module>
|
<module>struts2</module>
|
||||||
<module>apache-velocity</module>
|
<module>apache-velocity</module>
|
||||||
|
|
||||||
<module>testng</module>
|
|
||||||
|
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -43,6 +43,12 @@
|
|||||||
<filtering>true</filtering>
|
<filtering>true</filtering>
|
||||||
</resource>
|
</resource>
|
||||||
</resources>
|
</resources>
|
||||||
|
<testResources>
|
||||||
|
<testResource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</testResource>
|
||||||
|
</testResources>
|
||||||
|
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
||||||
|
@ -1,46 +1,25 @@
|
|||||||
package baeldung.com;
|
package baeldung.com;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.annotations.BeforeClass;
|
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
public class DependentTests {
|
public class DependentTests {
|
||||||
|
|
||||||
private String validEmail = "abc@qwe.com";
|
private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class);
|
||||||
private EmailValidator emailValidator;
|
|
||||||
private LoginValidator loginValidator;
|
|
||||||
|
|
||||||
@BeforeClass
|
private String email = "abc@qwe.com";
|
||||||
public void setup() {
|
|
||||||
emailValidator = new EmailValidator();
|
|
||||||
loginValidator = new LoginValidator();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmail_ifValid_thenTrue() {
|
public void givenEmail_ifValid_thenTrue() {
|
||||||
boolean valid = emailValidator.validate(validEmail);
|
boolean valid = email.contains("@");
|
||||||
Assert.assertEquals(valid, true);
|
Assert.assertEquals(valid, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" })
|
@Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"})
|
||||||
public void givenValidEmail_whenLoggedIn_thenTrue() {
|
public void givenValidEmail_whenLoggedIn_thenTrue() {
|
||||||
boolean valid = loginValidator.validate();
|
LOGGER.info("Email {} valid >> logging in", email);
|
||||||
Assert.assertEquals(valid, true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class EmailValidator {
|
|
||||||
|
|
||||||
boolean validate(String validEmail) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class LoginValidator {
|
|
||||||
|
|
||||||
boolean validate() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
package baeldung.com;
|
|
||||||
|
|
||||||
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 ParametrizedTestNGTest {
|
|
||||||
|
|
||||||
@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[][] { { "Peter", true }, { "Sam", true }, { "Tim", true }, { "Lucy", true } };
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(dataProvider = "test1")
|
|
||||||
public void givenNumber_whenPrime_thenCorrect(String name, boolean expectedResult) {
|
|
||||||
Assert.assertEquals(expectedResult, name.length() > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@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 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;
|
|
||||||
}
|
|
||||||
}
|
|
77
testng/src/test/java/baeldung/com/ParametrizedTests.java
Normal file
77
testng/src/test/java/baeldung/com/ParametrizedTests.java
Normal 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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,11 +1,14 @@
|
|||||||
package baeldung.com;
|
package baeldung.com;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
public class RegistrationTest {
|
public class RegistrationTest {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenEmail_ifValid_thenCorrect() {
|
public void whenCalledFromSuite_thanOK() {
|
||||||
|
LOGGER.info("Registration successful");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package baeldung.com;
|
package baeldung.com;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
public class SignInTest {
|
public class SignInTest {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsername_ifValid_thenCorrect() {
|
public void whenCalledFromSuite_thanOK() {
|
||||||
|
LOGGER.info("SignIn successful");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package baeldung.com;
|
package baeldung.com;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
import org.testng.TestNG;
|
import org.testng.TestNG;
|
||||||
import org.testng.annotations.*;
|
import org.testng.annotations.*;
|
||||||
@ -8,6 +10,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SummationServiceTest extends TestNG {
|
public class SummationServiceTest extends TestNG {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class);
|
||||||
|
|
||||||
private List<Integer> numbers;
|
private List<Integer> numbers;
|
||||||
|
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
<!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>
|
|
10
testng/src/test/resources/parametrized_test.xml
Normal file
10
testng/src/test/resources/parametrized_test.xml
Normal 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>
|
@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||||
<suite name="regression_test">
|
<suite name="regression_test">
|
||||||
<test name="testing">
|
<test name="test groups">
|
||||||
<groups>
|
<groups>
|
||||||
<run>
|
<run>
|
||||||
<include name="regression"/>
|
<include name="regression"/>
|
||||||
|
17
testng/src/test/resources/test_setup.xml
Normal file
17
testng/src/test/resources/test_setup.xml
Normal 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>
|
@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||||
<suite name="suite">
|
<suite name="suite">
|
||||||
<test name="suite">
|
<test name="test suite">
|
||||||
<classes>
|
<classes>
|
||||||
<class name="baeldung.com.RegistrationTest" />
|
<class name="baeldung.com.RegistrationTest" />
|
||||||
<class name="baeldung.com.SignInTest" />
|
<class name="baeldung.com.SignInTest" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user