From 32f2e4fa71c6a53bdb1a00ff271be6b724603238 Mon Sep 17 00:00:00 2001 From: "igor.vlahek" Date: Sun, 1 Dec 2019 08:57:47 +0100 Subject: [PATCH 1/3] c --- .../core/runner/ZeroCodeUnitRunner.java | 72 +++++++++++++------ 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java index 3ca94431c..fccc43458 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java +++ b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java @@ -3,21 +3,10 @@ import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.util.Modules; -import java.lang.annotation.Annotation; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; import org.jsmart.zerocode.core.di.main.ApplicationMainModule; import org.jsmart.zerocode.core.di.module.RuntimeHttpClientModule; import org.jsmart.zerocode.core.di.module.RuntimeKafkaClientModule; -import org.jsmart.zerocode.core.domain.HostProperties; -import org.jsmart.zerocode.core.domain.JsonTestCase; -import org.jsmart.zerocode.core.domain.Scenario; -import org.jsmart.zerocode.core.domain.ScenarioSpec; -import org.jsmart.zerocode.core.domain.TargetEnv; -import org.jsmart.zerocode.core.domain.UseHttpClient; -import org.jsmart.zerocode.core.domain.UseKafkaClient; +import org.jsmart.zerocode.core.domain.*; import org.jsmart.zerocode.core.domain.builders.ZeroCodeExecResultBuilder; import org.jsmart.zerocode.core.domain.builders.ZeroCodeExecResultIoWriteBuilder; import org.jsmart.zerocode.core.engine.listener.ZeroCodeTestReportListener; @@ -30,6 +19,8 @@ import org.jsmart.zerocode.core.utils.SmartUtils; import org.junit.internal.AssumptionViolatedException; import org.junit.internal.runners.model.EachTestNotifier; +import org.junit.rules.RunRules; +import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.notification.Failure; @@ -41,6 +32,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.lang.annotation.Annotation; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + import static java.lang.System.getProperty; import static org.jsmart.zerocode.core.domain.builders.ZeroCodeExecResultBuilder.newInstance; import static org.jsmart.zerocode.core.domain.reports.ZeroCodeReportProperties.CHARTS_AND_CSV; @@ -113,7 +110,7 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) { final Description description = describeChild(method); JsonTestCase jsonTestCaseAnno = method.getMethod().getAnnotation(JsonTestCase.class); - if(jsonTestCaseAnno == null){ + if (jsonTestCaseAnno == null) { jsonTestCaseAnno = evalScenarioToJsonTestCase(method.getMethod().getAnnotation(Scenario.class)); } @@ -122,8 +119,8 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) { notifier.fireTestIgnored(description); } else if (jsonTestCaseAnno != null) { - - runLeafJsonTest(notifier, description, jsonTestCaseAnno); + Statement statement = methodBlock(method); + runLeafJsonTest(statement, notifier, description, jsonTestCaseAnno); } else { // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= @@ -134,6 +131,38 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) { } + + private Statement withRules(FrameworkMethod method, Object target, + Statement statement) { + List testRules = getTestRules(target); + Statement result = statement; + result = withMethodRules(method, testRules, target, result); + result = withTestRules(method, testRules, result); + + return result; + } + + private Statement withTestRules(FrameworkMethod method, List testRules, + Statement statement) { + return testRules.isEmpty() ? statement : + new RunRules(statement, testRules, describeChild(method)); + } + + private Statement withMethodRules(FrameworkMethod method, List testRules, + Object target, Statement result) { + for (org.junit.rules.MethodRule each : getMethodRules(target)) { + if (!testRules.contains(each)) { + result = each.apply(result, method, target); + } + } + return result; + } + + private List getMethodRules(Object target) { + return rules(target); + } + + public List getSmartTestCaseNames() { return smartTestCaseNames; } @@ -188,7 +217,7 @@ protected ZeroCodeReportGenerator getInjectedReportGenerator() { return getMainModuleInjector().getInstance(ZeroCodeReportGenerator.class); } - private void runLeafJsonTest(RunNotifier notifier, Description description, JsonTestCase jsonTestCaseAnno) { + private void runLeafJsonTest(Statement statement, RunNotifier notifier, Description description, JsonTestCase jsonTestCaseAnno) { if (jsonTestCaseAnno != null) { currentTestCase = jsonTestCaseAnno.value(); } @@ -196,7 +225,7 @@ private void runLeafJsonTest(RunNotifier notifier, Description description, Json notifier.fireTestStarted(description); LOGGER.debug("### Running currentTestCase : " + currentTestCase); - + EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description); ScenarioSpec child = null; try { child = smartUtils.jsonFileToJava(currentTestCase, ScenarioSpec.class); @@ -204,8 +233,8 @@ private void runLeafJsonTest(RunNotifier notifier, Description description, Json LOGGER.debug("### Found currentTestCase : -" + child); passed = multiStepsRunner.runScenario(child, notifier, description); - - } catch (Exception ioEx) { + statement.evaluate(); + } catch (Throwable ioEx) { ioEx.printStackTrace(); notifier.fireTestFailure(new Failure(description, ioEx)); } @@ -229,7 +258,7 @@ private List getSmartChildrenList() { frameworkMethod -> { JsonTestCase jsonTestCaseAnno = frameworkMethod.getAnnotation(JsonTestCase.class); - if(jsonTestCaseAnno == null){ + if (jsonTestCaseAnno == null) { jsonTestCaseAnno = evalScenarioToJsonTestCase(frameworkMethod.getAnnotation(Scenario.class)); } @@ -294,6 +323,7 @@ private final void runLeafJUnitTest(Statement statement, Description description } } + private void buildReportAndPrintToFile(Description description) { ZeroCodeExecResultBuilder reportResultBuilder = newInstance().loop(0).scenarioName(description.getClassName()); reportResultBuilder.step(logCorrelationshipPrinter.buildReportSingleStep()); @@ -359,7 +389,7 @@ public Class annotationType() { @Override public String value() { - return scenario != null? scenario.value(): null; + return scenario != null ? scenario.value() : null; } }; From c03f8d3040632113199b0ab5533885c95f2b215b Mon Sep 17 00:00:00 2001 From: "igor.vlahek" Date: Sat, 22 Feb 2020 20:56:24 +0100 Subject: [PATCH 2/3] resolved --- core/pom.xml | 2 +- .../core/runner/ZeroCodeUnitRunner.java | 101 ++++++++++-------- http-testing/pom.xml | 2 +- junit5-testing/pom.xml | 2 +- kafka-testing/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 63 insertions(+), 48 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index 13c28f26b..dfa549a5d 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -4,7 +4,7 @@ zerocode-tdd-parent org.jsmart - 1.3.17-SNAPSHOT + 1.3.17-RULE zerocode-tdd diff --git a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java index 58c778c8a..eb8f43359 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java +++ b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java @@ -6,25 +6,19 @@ import org.jsmart.zerocode.core.di.main.ApplicationMainModule; import org.jsmart.zerocode.core.di.module.RuntimeHttpClientModule; import org.jsmart.zerocode.core.di.module.RuntimeKafkaClientModule; -import org.jsmart.zerocode.core.domain.HostProperties; -import org.jsmart.zerocode.core.domain.JsonTestCase; -import org.jsmart.zerocode.core.domain.Scenario; -import org.jsmart.zerocode.core.domain.ScenarioSpec; -import org.jsmart.zerocode.core.domain.TargetEnv; -import org.jsmart.zerocode.core.domain.UseHttpClient; -import org.jsmart.zerocode.core.domain.UseKafkaClient; +import org.jsmart.zerocode.core.domain.*; import org.jsmart.zerocode.core.domain.builders.ZeroCodeExecReportBuilder; import org.jsmart.zerocode.core.domain.builders.ZeroCodeIoWriteBuilder; import org.jsmart.zerocode.core.engine.listener.ZeroCodeTestReportListener; import org.jsmart.zerocode.core.httpclient.BasicHttpClient; -import org.jsmart.zerocode.core.httpclient.ssl.SslTrustHttpClient; import org.jsmart.zerocode.core.kafka.client.BasicKafkaClient; -import org.jsmart.zerocode.core.kafka.client.ZerocodeCustomKafkaClient; import org.jsmart.zerocode.core.logbuilder.ZerocodeCorrelationshipLogger; import org.jsmart.zerocode.core.report.ZeroCodeReportGenerator; import org.jsmart.zerocode.core.utils.SmartUtils; import org.junit.internal.AssumptionViolatedException; import org.junit.internal.runners.model.EachTestNotifier; +import org.junit.internal.runners.model.ReflectiveCallable; +import org.junit.internal.runners.statements.Fail; import org.junit.rules.RunRules; import org.junit.rules.TestRule; import org.junit.runner.Description; @@ -45,12 +39,10 @@ import java.util.stream.Collectors; import static java.lang.System.getProperty; -import static org.jsmart.zerocode.core.domain.builders.ZeroCodeExecReportBuilder.newInstance; import static org.jsmart.zerocode.core.constants.ZeroCodeReportConstants.CHARTS_AND_CSV; import static org.jsmart.zerocode.core.constants.ZeroCodeReportConstants.ZEROCODE_JUNIT; -import static org.jsmart.zerocode.core.utils.RunnerUtils.getCustomHttpClientOrDefault; -import static org.jsmart.zerocode.core.utils.RunnerUtils.getCustomKafkaClientOrDefault; -import static org.jsmart.zerocode.core.utils.RunnerUtils.getEnvSpecificConfigFile; +import static org.jsmart.zerocode.core.domain.builders.ZeroCodeExecReportBuilder.newInstance; +import static org.jsmart.zerocode.core.utils.RunnerUtils.*; public class ZeroCodeUnitRunner extends BlockJUnit4ClassRunner { private static final Logger LOGGER = LoggerFactory.getLogger(ZeroCodeUnitRunner.class); @@ -127,8 +119,7 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) { notifier.fireTestIgnored(description); } else if (jsonTestCaseAnno != null) { - Statement statement = methodBlock(method); - runLeafJsonTest(statement, notifier, description, jsonTestCaseAnno); + runLeafJsonTest(method, notifier, description, jsonTestCaseAnno); } else { // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= @@ -139,7 +130,6 @@ protected void runChild(FrameworkMethod method, RunNotifier notifier) { } - private Statement withRules(FrameworkMethod method, Object target, Statement statement) { List testRules = getTestRules(target); @@ -170,7 +160,6 @@ private List getMethodRules(Object target) { return rules(target); } - public List getSmartTestCaseNames() { return smartTestCaseNames; } @@ -214,39 +203,65 @@ protected ZeroCodeReportGenerator getInjectedReportGenerator() { return getMainModuleInjector().getInstance(ZeroCodeReportGenerator.class); } - private void runLeafJsonTest(Statement statement, RunNotifier notifier, Description description, JsonTestCase jsonTestCaseAnno) { - if (jsonTestCaseAnno != null) { - currentTestCase = jsonTestCaseAnno.value(); + private void runLeafJsonTest(FrameworkMethod method, RunNotifier notifier, Description description, JsonTestCase jsonTestCaseAnno) { + Object test; + try { + test = new ReflectiveCallable() { + @Override + protected Object runReflectiveCall() throws Throwable { + return createTest(); + } + }.run(); + } catch (Throwable e) { + return; } - notifier.fireTestStarted(description); + Statement statement = new Statement() { + @Override + public void evaluate() throws Throwable { + if (jsonTestCaseAnno != null) { + currentTestCase = jsonTestCaseAnno.value(); + } + notifier.fireTestStarted(description); + + LOGGER.debug("### Running currentTestCase : " + currentTestCase); + ScenarioSpec child = null; + try { + child = smartUtils.scenarioFileToJava(currentTestCase, ScenarioSpec.class); + + LOGGER.debug("### Found currentTestCase : -" + child); + + passed = multiStepsRunner.runScenario(child, notifier, description); + } catch (Throwable ioEx) { + ioEx.printStackTrace(); + notifier.fireTestFailure(new Failure(description, ioEx)); + throw ioEx; + } finally { + testRunCompleted = true; + + if (passed) { + LOGGER.info(String.format("\n**FINISHED executing all Steps for [%s] **.\nSteps were:%s", + child.getScenarioName(), + child.getSteps().stream() + .map(step -> step.getName() == null ? step.getId() : step.getName()) + .collect(Collectors.toList()))); + } - LOGGER.debug("### Running currentTestCase : " + currentTestCase); - EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description); - ScenarioSpec child = null; + notifier.fireTestFinished(description); + throw new RuntimeException(); + } + } + }; + statement = possiblyExpectingExceptions(method, test, statement); + statement = withPotentialTimeout(method, test, statement); + statement = withBefores(method, test, statement); + statement = withAfters(method, test, statement); + statement = withRules(method, test, statement); try { - child = smartUtils.scenarioFileToJava(currentTestCase, ScenarioSpec.class); - - LOGGER.debug("### Found currentTestCase : -" + child); - - passed = multiStepsRunner.runScenario(child, notifier, description); statement.evaluate(); - } catch (Throwable ioEx) { - ioEx.printStackTrace(); - notifier.fireTestFailure(new Failure(description, ioEx)); - } - - testRunCompleted = true; - - if (passed) { - LOGGER.info(String.format("\n**FINISHED executing all Steps for [%s] **.\nSteps were:%s", - child.getScenarioName(), - child.getSteps().stream() - .map(step -> step.getName() == null ? step.getId() : step.getName()) - .collect(Collectors.toList()))); + } catch (Throwable e) { } - notifier.fireTestFinished(description); } private List getSmartChildrenList() { diff --git a/http-testing/pom.xml b/http-testing/pom.xml index 5800aefa7..799e3df12 100644 --- a/http-testing/pom.xml +++ b/http-testing/pom.xml @@ -4,7 +4,7 @@ zerocode-tdd-parent org.jsmart - 1.3.17-SNAPSHOT + 1.3.17-RULE org.jsmart diff --git a/junit5-testing/pom.xml b/junit5-testing/pom.xml index 598602109..b1720348f 100644 --- a/junit5-testing/pom.xml +++ b/junit5-testing/pom.xml @@ -4,7 +4,7 @@ zerocode-tdd-parent org.jsmart - 1.3.17-SNAPSHOT + 1.3.17-RULE zerocode-tdd-jupiter diff --git a/kafka-testing/pom.xml b/kafka-testing/pom.xml index 9d6b4898c..ec7bd1a8e 100644 --- a/kafka-testing/pom.xml +++ b/kafka-testing/pom.xml @@ -4,7 +4,7 @@ zerocode-tdd-parent org.jsmart - 1.3.17-SNAPSHOT + 1.3.17-RULE kafka-testing diff --git a/pom.xml b/pom.xml index 3ed0f0a97..e1932ccce 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ zerocode-tdd-parent org.jsmart - 1.3.17-SNAPSHOT + 1.3.17-RULE pom ZeroCode TDD Parent From 39f3f7c1b88963a4db7c1a060c7caf77768b9d53 Mon Sep 17 00:00:00 2001 From: "igor.vlahek" Date: Sun, 23 Feb 2020 19:55:00 +0100 Subject: [PATCH 3/3] ISSUE-365 # Support for standard JUnit annotations added --- .../core/runner/ZeroCodeUnitRunner.java | 27 +++++++----- .../zerocode/annotations/TestBeforeAfter.java | 44 +++++++++++++++++++ .../annotations/assert_before.json | 11 +++++ 3 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 core/src/test/java/org/jsmart/zerocode/annotations/TestBeforeAfter.java create mode 100644 core/src/test/resources/integration_test_files/annotations/assert_before.json diff --git a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java index eb8f43359..7000f32ef 100644 --- a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java +++ b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java @@ -216,7 +216,21 @@ protected Object runReflectiveCall() throws Throwable { return; } - Statement statement = new Statement() { + Statement statement = createZeroCodeStatement(jsonTestCaseAnno, notifier, description); + statement = possiblyExpectingExceptions(method, test, statement); + statement = withPotentialTimeout(method, test, statement); + statement = withBefores(method, test, statement); + statement = withAfters(method, test, statement); + statement = withRules(method, test, statement); + try { + statement.evaluate(); + } catch (Throwable e) { + } + + } + + private Statement createZeroCodeStatement(JsonTestCase jsonTestCaseAnno, RunNotifier notifier, Description description) { + return new Statement() { @Override public void evaluate() throws Throwable { if (jsonTestCaseAnno != null) { @@ -248,20 +262,9 @@ public void evaluate() throws Throwable { } notifier.fireTestFinished(description); - throw new RuntimeException(); } } }; - statement = possiblyExpectingExceptions(method, test, statement); - statement = withPotentialTimeout(method, test, statement); - statement = withBefores(method, test, statement); - statement = withAfters(method, test, statement); - statement = withRules(method, test, statement); - try { - statement.evaluate(); - } catch (Throwable e) { - } - } private List getSmartChildrenList() { diff --git a/core/src/test/java/org/jsmart/zerocode/annotations/TestBeforeAfter.java b/core/src/test/java/org/jsmart/zerocode/annotations/TestBeforeAfter.java new file mode 100644 index 000000000..b32fe8565 --- /dev/null +++ b/core/src/test/java/org/jsmart/zerocode/annotations/TestBeforeAfter.java @@ -0,0 +1,44 @@ +package org.jsmart.zerocode.annotations; + +import org.jsmart.zerocode.core.domain.JsonTestCase; +import org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; + +@RunWith(ZeroCodeUnitRunner.class) +public class TestBeforeAfter { + + private static int variable = 0; + + @Before + public void setUp() { + assertEquals(0, variable); + variable = 1; + } + + @After + public void tearDown() { + assertEquals(1, variable); + variable = 0; + } + + public void assertBeforeIsCalled(String request) { + assertEquals(1, variable); + } + + @Test + @JsonTestCase("integration_test_files/annotations/assert_before.json") + public void test_before_is_called_1() throws Exception { + + } + + @Test + @JsonTestCase("integration_test_files/annotations/assert_before.json") + public void test_before_is_called_2() throws Exception { + + } +} diff --git a/core/src/test/resources/integration_test_files/annotations/assert_before.json b/core/src/test/resources/integration_test_files/annotations/assert_before.json new file mode 100644 index 000000000..5cf305485 --- /dev/null +++ b/core/src/test/resources/integration_test_files/annotations/assert_before.json @@ -0,0 +1,11 @@ +{ + "scenarioName": "Testing @Before, @After annotations", + "steps": [ + { + "name": "assert_step", + "url": "org.jsmart.zerocode.annotations.TestBeforeAfter", + "operation": "assertBeforeIsCalled", + "assertions": {} + } + ] +} \ No newline at end of file