Group testing modules (#3014)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules * group testing modules * try fix conflict
This commit is contained in:
committed by
GitHub
parent
b383d83bf4
commit
776a01429e
@@ -0,0 +1,25 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class DependentLongRunningUnitTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DependentLongRunningUnitTest.class);
|
||||
|
||||
private String email = "abc@qwe.com";
|
||||
|
||||
@Test
|
||||
public void givenEmail_ifValid_thenTrue() {
|
||||
boolean valid = email.contains("@");
|
||||
Assert.assertEquals(valid, true);
|
||||
}
|
||||
|
||||
@Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"})
|
||||
public void givenValidEmail_whenLoggedIn_thenTrue() {
|
||||
LOGGER.info("Email {} valid >> logging in", email);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.testng.annotations.AfterGroups;
|
||||
import org.testng.annotations.BeforeGroups;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class GroupIntegrationTest {
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class MultiThreadedIntegrationTest {
|
||||
|
||||
@Test(threadPoolSize = 5, invocationCount = 10, timeOut = 1000)
|
||||
public void givenMethod_whenRunInThreads_thenCorrect() {
|
||||
int count = Thread.activeCount();
|
||||
Assert.assertTrue(count > 1);
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Parameters;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
public class ParametrizedLongRunningUnitTest {
|
||||
|
||||
@Test
|
||||
@Parameters({"value", "isEven"})
|
||||
public void givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) {
|
||||
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) {
|
||||
assertEquals(expected, number % 2 == 0);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "numbersObject")
|
||||
public void givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect(EvenNumber number) {
|
||||
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;
|
||||
|
||||
EvenNumber(int number, boolean isEven) {
|
||||
this.value = number;
|
||||
this.isEven = isEven;
|
||||
}
|
||||
|
||||
int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
boolean isEven() {
|
||||
return isEven;
|
||||
}
|
||||
|
||||
public void setEven(boolean even) {
|
||||
isEven = even;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EvenNumber{" +
|
||||
"value=" + value +
|
||||
", isEven=" + isEven +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
public class PriorityLongRunningUnitTest {
|
||||
|
||||
@Test(priority = 1)
|
||||
public void givenString_whenChangedToInt_thenCorrect() {
|
||||
String testString = "10";
|
||||
assertTrue(Integer.valueOf(testString) instanceof Integer);
|
||||
}
|
||||
|
||||
@Test(priority = 2)
|
||||
public void givenInt_whenChangedToString_thenCorrect() {
|
||||
int testInt = 23;
|
||||
assertTrue(String.valueOf(testInt) instanceof String);
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class RegistrationLongRunningUnitTest {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationLongRunningUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void whenCalledFromSuite_thanOK() {
|
||||
LOGGER.info("Registration successful");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class SignInLongRunningUnitTest {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SignInLongRunningUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void whenCalledFromSuite_thanOK() {
|
||||
LOGGER.info("SignIn successful");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.TestNG;
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class SimpleLongRunningUnitTest extends TestNG {
|
||||
private int number;
|
||||
|
||||
@BeforeClass
|
||||
public void setup() {
|
||||
number = 12;
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public void tearDown() {
|
||||
number = 0;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumber_whenEven_thenTrue() {
|
||||
Assert.assertTrue(number % 2 == 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
package com.baeldung;
|
||||
|
||||
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.AfterSuite;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeGroups;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.BeforeSuite;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SummationServiceIntegrationTest extends TestNG {
|
||||
private List<Integer> numbers;
|
||||
|
||||
private int testCount = 0;
|
||||
|
||||
@BeforeClass
|
||||
public void initialize() {
|
||||
numbers = new ArrayList<>();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public void tearDown() {
|
||||
numbers = null;
|
||||
}
|
||||
|
||||
@BeforeSuite(groups = "regression")
|
||||
public void runBeforeRegressionSuite() {
|
||||
numbers = new ArrayList<>();
|
||||
numbers.add(-11);
|
||||
numbers.add(2);
|
||||
}
|
||||
|
||||
@AfterSuite(groups = "regression")
|
||||
public void runAfterRegressionSuite() {
|
||||
numbers = null;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@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);
|
||||
Assert.assertEquals(sum, 6);
|
||||
}
|
||||
|
||||
@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(expectedExceptions = ArithmeticException.class)
|
||||
public void givenNumber_whenThrowsException_thenCorrect() {
|
||||
int i = 1 / 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class TimeOutIntegrationTest {
|
||||
|
||||
@Test(timeOut = 1000, enabled = false)
|
||||
public void givenExecution_takeMoreTime_thenFail() {
|
||||
while (true) ;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.reports;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.ITestContext;
|
||||
import org.testng.ITestListener;
|
||||
import org.testng.ITestResult;
|
||||
|
||||
public class CustomisedListener implements ITestListener {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger("CUSTOM_LOGS");
|
||||
|
||||
@Override
|
||||
public void onFinish(ITestContext context) {
|
||||
LOGGER.info("PASSED TEST CASES");
|
||||
context.getPassedTests()
|
||||
.getAllResults()
|
||||
.forEach(result -> {
|
||||
LOGGER.info(result.getName());
|
||||
});
|
||||
LOGGER.info("FAILED TEST CASES");
|
||||
context.getFailedTests()
|
||||
.getAllResults()
|
||||
.forEach(result -> {
|
||||
LOGGER.info(result.getName());
|
||||
});
|
||||
LOGGER.info("Test completed on: " + context.getEndDate()
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(ITestContext arg0) {
|
||||
LOGGER.info("Started testing on: " + arg0.getStartDate()
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestFailure(ITestResult arg0) {
|
||||
LOGGER.info("Failed : " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestSkipped(ITestResult arg0) {
|
||||
LOGGER.info("Skipped Test: " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestStart(ITestResult arg0) {
|
||||
LOGGER.info("Testing: " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestSuccess(ITestResult arg0) {
|
||||
long timeTaken = ((arg0.getEndMillis() - arg0.getStartMillis()));
|
||||
LOGGER.info("Tested: " + arg0.getName() + " Time taken:" + timeTaken + " ms");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.baeldung.reports;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.*;
|
||||
import org.testng.xml.XmlSuite;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class CustomisedReports implements IReporter {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class);
|
||||
|
||||
private static final String ROW_TEMPLATE = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
|
||||
|
||||
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
|
||||
String reportTemplate = initReportTemplate();
|
||||
|
||||
final String body = suites
|
||||
.stream()
|
||||
.flatMap(suiteToResults())
|
||||
.collect(Collectors.joining());
|
||||
|
||||
saveReportTemplate(outputDirectory, reportTemplate.replaceFirst("</tbody>", String.format("%s</tbody>", body)));
|
||||
}
|
||||
|
||||
private Function<ISuite, Stream<? extends String>> suiteToResults() {
|
||||
return suite -> suite.getResults().entrySet()
|
||||
.stream()
|
||||
.flatMap(resultsToRows(suite));
|
||||
}
|
||||
|
||||
private Function<Map.Entry<String, ISuiteResult>, Stream<? extends String>> resultsToRows(ISuite suite) {
|
||||
return e -> {
|
||||
ITestContext testContext = e.getValue().getTestContext();
|
||||
|
||||
Set<ITestResult> failedTests = testContext
|
||||
.getFailedTests()
|
||||
.getAllResults();
|
||||
Set<ITestResult> passedTests = testContext
|
||||
.getPassedTests()
|
||||
.getAllResults();
|
||||
Set<ITestResult> skippedTests = testContext
|
||||
.getSkippedTests()
|
||||
.getAllResults();
|
||||
|
||||
String suiteName = suite.getName();
|
||||
|
||||
return Stream
|
||||
.of(failedTests, passedTests, skippedTests)
|
||||
.flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream());
|
||||
};
|
||||
}
|
||||
|
||||
private List<String> generateReportRows(String testName, String suiteName, Set<ITestResult> allTestResults) {
|
||||
return allTestResults.stream()
|
||||
.map(testResultToResultRow(testName, suiteName))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
private Function<ITestResult, String> testResultToResultRow(String testName, String suiteName) {
|
||||
return testResult -> {
|
||||
switch (testResult.getStatus()) {
|
||||
case ITestResult.FAILURE:
|
||||
return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
|
||||
|
||||
case ITestResult.SUCCESS:
|
||||
return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
|
||||
|
||||
case ITestResult.SKIP:
|
||||
return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String initReportTemplate() {
|
||||
String template = null;
|
||||
byte[] reportTemplate;
|
||||
try {
|
||||
reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html"));
|
||||
template = new String(reportTemplate, "UTF-8");
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Problem initializing template", e);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
private void saveReportTemplate(String outputDirectory, String reportTemplate) {
|
||||
new File(outputDirectory).mkdirs();
|
||||
try {
|
||||
PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html"))));
|
||||
reportWriter.println(reportTemplate);
|
||||
reportWriter.flush();
|
||||
reportWriter.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Problem saving template", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user