From 7d48d09973f682a65522fee1c1d7063b70b34602 Mon Sep 17 00:00:00 2001 From: Olha Omelianchuk Date: Wed, 10 Jul 2024 13:57:47 +0200 Subject: [PATCH] [#266] clear test context after run --- .../junit5/TemplateInvocationContext.java | 2 ++ .../data/ClearedContextBetweenMethods.java | 28 +++++++++++++++++++ .../data/CleanedContextBetweenMethods.java | 25 +++++++++++++++++ .../data/CleanedContextBetweenMethods.json | 11 ++++++++ .../data/CleanedContextBetweenMethods.json | 11 ++++++++ 5 files changed, 77 insertions(+) create mode 100644 src/test/java/com/xceptance/neodymium/junit4/testclasses/data/ClearedContextBetweenMethods.java create mode 100644 src/test/java/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.java create mode 100644 src/test/resources/com/xceptance/neodymium/junit4/testclasses/data/CleanedContextBetweenMethods.json create mode 100644 src/test/resources/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.json diff --git a/src/main/java/com/xceptance/neodymium/junit5/TemplateInvocationContext.java b/src/main/java/com/xceptance/neodymium/junit5/TemplateInvocationContext.java index 3ec39a4bd..79b7ba609 100644 --- a/src/main/java/com/xceptance/neodymium/junit5/TemplateInvocationContext.java +++ b/src/main/java/com/xceptance/neodymium/junit5/TemplateInvocationContext.java @@ -12,6 +12,7 @@ import com.xceptance.neodymium.junit5.filtering.FilterTestMethodCallback; import com.xceptance.neodymium.junit5.filtering.WipTestMethodCallback; import com.xceptance.neodymium.junit5.testdata.TestdataCallback; +import com.xceptance.neodymium.util.Neodymium; public class TemplateInvocationContext implements TestTemplateInvocationContext { @@ -40,6 +41,7 @@ public String getDisplayName(int invocationIndex) @Override public List getAdditionalExtensions() { + Neodymium.clearThreadContext(); List extentions = new LinkedList<>(); if (browser != null) { diff --git a/src/test/java/com/xceptance/neodymium/junit4/testclasses/data/ClearedContextBetweenMethods.java b/src/test/java/com/xceptance/neodymium/junit4/testclasses/data/ClearedContextBetweenMethods.java new file mode 100644 index 000000000..f390ea8aa --- /dev/null +++ b/src/test/java/com/xceptance/neodymium/junit4/testclasses/data/ClearedContextBetweenMethods.java @@ -0,0 +1,28 @@ +package com.xceptance.neodymium.junit4.testclasses.data; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.xceptance.neodymium.junit4.NeodymiumRunner; +import com.xceptance.neodymium.util.DataUtils; + +@RunWith(NeodymiumRunner.class) +public class ClearedContextBetweenMethods +{ + @Test + public void test() + { + if (DataUtils.asString("testId").equals("fist set")) + { + Assert.assertEquals("Test data is not matching the test expectations", "val1", DataUtils.asString("key1")); + Assert.assertEquals("Test data is not matching the test expectations", "val2", DataUtils.asString("key2")); + } + else + { + Assert.assertEquals("Test data is not overwritten", "new val", DataUtils.asString("key1")); + Assert.assertNull("Test data context is not cleared", DataUtils.asString("key2", null)); + } + } + +} diff --git a/src/test/java/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.java b/src/test/java/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.java new file mode 100644 index 000000000..24ccf09f7 --- /dev/null +++ b/src/test/java/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.java @@ -0,0 +1,25 @@ +package com.xceptance.neodymium.junit5.testclasses.data; + +import org.junit.Assert; + +import com.xceptance.neodymium.junit5.NeodymiumTest; +import com.xceptance.neodymium.util.DataUtils; + +public class CleanedContextBetweenMethods +{ + @NeodymiumTest + public void test() + { + if (DataUtils.asString("testId").equals("fist set")) + { + Assert.assertEquals("Test data is not matching the test expectations", "val1", DataUtils.asString("key1")); + Assert.assertEquals("Test data is not matching the test expectations", "val2", DataUtils.asString("key2")); + } + else + { + Assert.assertEquals("Test data is not overwritten", "new val", DataUtils.asString("key1")); + Assert.assertNull("Test data context is not cleared", DataUtils.asString("key2", null)); + } + } + +} diff --git a/src/test/resources/com/xceptance/neodymium/junit4/testclasses/data/CleanedContextBetweenMethods.json b/src/test/resources/com/xceptance/neodymium/junit4/testclasses/data/CleanedContextBetweenMethods.json new file mode 100644 index 000000000..31619e85f --- /dev/null +++ b/src/test/resources/com/xceptance/neodymium/junit4/testclasses/data/CleanedContextBetweenMethods.json @@ -0,0 +1,11 @@ +[ + { + "testId": "fist set", + "key1": "val1", + "key2": "val2" + }, + { + "testId": "second set", + "key1": "new val" + } +] diff --git a/src/test/resources/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.json b/src/test/resources/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.json new file mode 100644 index 000000000..31619e85f --- /dev/null +++ b/src/test/resources/com/xceptance/neodymium/junit5/testclasses/data/CleanedContextBetweenMethods.json @@ -0,0 +1,11 @@ +[ + { + "testId": "fist set", + "key1": "val1", + "key2": "val2" + }, + { + "testId": "second set", + "key1": "new val" + } +]