From cb1fa3e34747e06e8b167d6dde52716180229ffc Mon Sep 17 00:00:00 2001 From: Andreas Schmid Date: Sun, 1 Apr 2018 23:59:43 +0200 Subject: [PATCH 1/2] add test cases for implementing test cases for junit-jupiter* (#106) * both are working correctly as JUnit5 filters bridge / synthetic methods, see `ReflectionUtlis` * in addition restructored abstract base class tests a little Signed-off-by: Andreas Schmid --- ...t.java => AbstractAcceptanceBaseTest.java} | 2 +- .../override/AcceptanceTestInterface.java | 8 ++++ .../override/ImplementingAcceptanceTest.java | 40 +++++++++++++++++ .../override/OverridingAcceptanceTest.java | 2 +- ...t.java => AbstractAcceptanceBaseTest.java} | 2 +- .../override/AcceptanceTestInterface.java | 8 ++++ .../override/ImplementingAcceptanceTest.java | 43 +++++++++++++++++++ .../override/OverridingAcceptanceTest.java | 2 +- 8 files changed, 103 insertions(+), 4 deletions(-) rename junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/{ToBeOverriddenAcceptanceTest.java => AbstractAcceptanceBaseTest.java} (94%) create mode 100644 junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java create mode 100644 junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java rename junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/{ToBeOverriddenAcceptanceTest.java => AbstractAcceptanceBaseTest.java} (94%) create mode 100644 junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java create mode 100644 junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java diff --git a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ToBeOverriddenAcceptanceTest.java b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java similarity index 94% rename from junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ToBeOverriddenAcceptanceTest.java rename to junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java index ac040771..754e79e6 100644 --- a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ToBeOverriddenAcceptanceTest.java +++ b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java @@ -8,7 +8,7 @@ import com.tngtech.junit.dataprovider.DataProvider; import com.tngtech.junit.dataprovider.UseDataProvider; -abstract class ToBeOverriddenAcceptanceTest { +abstract class AbstractAcceptanceBaseTest { @DataProvider static Object[][] dataProviderBase() { diff --git a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java new file mode 100644 index 00000000..7a7d72d9 --- /dev/null +++ b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java @@ -0,0 +1,8 @@ +package com.tngtech.test.junit.dataprovider.override; + +public interface AcceptanceTestInterface { + + void testToBeImplemented(); + + void testToBeImplemented(T value); +} diff --git a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java new file mode 100644 index 00000000..b8585979 --- /dev/null +++ b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java @@ -0,0 +1,40 @@ +package com.tngtech.test.junit.dataprovider.override; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; + +import com.tngtech.junit.dataprovider.DataProvider; +import com.tngtech.junit.dataprovider.UseDataProvider; + +class ImplementingAcceptanceTest implements AcceptanceTestInterface { + + private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0); + private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0); + + @DataProvider + public static Object[][] dataProviderToBeImplemented() { + // @formatter:off + return new Object[][] { + { 1 }, + { 2 }, + }; + // @formatter:on + } + + @Test + @Override + public void testToBeImplemented() { + assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet()); + } + + @ParameterizedTest + @UseDataProvider + @Override + public void testToBeImplemented(Integer value) { + assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet()); + } +} diff --git a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java index 62c62325..cc1a0a49 100644 --- a/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java +++ b/junit-jupiter-params/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java @@ -7,7 +7,7 @@ import com.tngtech.junit.dataprovider.DataProvider; import com.tngtech.junit.dataprovider.UseDataProvider; -class OverridingAcceptanceTest extends ToBeOverriddenAcceptanceTest { +class OverridingAcceptanceTest extends AbstractAcceptanceBaseTest { @Override @ParameterizedTest diff --git a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ToBeOverriddenAcceptanceTest.java b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java similarity index 94% rename from junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ToBeOverriddenAcceptanceTest.java rename to junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java index 8bc89e80..d7a63c48 100644 --- a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ToBeOverriddenAcceptanceTest.java +++ b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AbstractAcceptanceBaseTest.java @@ -8,7 +8,7 @@ import com.tngtech.junit.dataprovider.DataProvider; import com.tngtech.junit.dataprovider.UseDataProvider; -abstract class ToBeOverriddenAcceptanceTest { +abstract class AbstractAcceptanceBaseTest { @DataProvider static Object[][] dataProviderBase() { diff --git a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java new file mode 100644 index 00000000..7a7d72d9 --- /dev/null +++ b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/AcceptanceTestInterface.java @@ -0,0 +1,8 @@ +package com.tngtech.test.junit.dataprovider.override; + +public interface AcceptanceTestInterface { + + void testToBeImplemented(); + + void testToBeImplemented(T value); +} diff --git a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java new file mode 100644 index 00000000..c7d6d8d8 --- /dev/null +++ b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/ImplementingAcceptanceTest.java @@ -0,0 +1,43 @@ +package com.tngtech.test.junit.dataprovider.override; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; + +import com.tngtech.junit.dataprovider.DataProvider; +import com.tngtech.junit.dataprovider.UseDataProvider; +import com.tngtech.junit.dataprovider.UseDataProviderExtension; + +@ExtendWith(UseDataProviderExtension.class) +class ImplementingAcceptanceTest implements AcceptanceTestInterface { + + private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0); + private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0); + + @DataProvider + public static Object[][] dataProviderToBeImplemented() { + // @formatter:off + return new Object[][] { + { 1 }, + { 2 }, + }; + // @formatter:on + } + + @Test + @Override + public void testToBeImplemented() { + assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet()); + } + + @TestTemplate + @UseDataProvider + @Override + public void testToBeImplemented(Integer value) { + assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet()); + } +} diff --git a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java index 45b6f7a3..169d2ec5 100644 --- a/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java +++ b/junit-jupiter/src/integTest/java/com/tngtech/test/junit/dataprovider/override/OverridingAcceptanceTest.java @@ -10,7 +10,7 @@ import com.tngtech.junit.dataprovider.UseDataProviderExtension; @ExtendWith(UseDataProviderExtension.class) -class OverridingAcceptanceTest extends ToBeOverriddenAcceptanceTest { +class OverridingAcceptanceTest extends AbstractAcceptanceBaseTest { @Override @TestTemplate From 93b5da4a666d5f19b7f7b86dd77ef07603ee2814 Mon Sep 17 00:00:00 2001 From: Andreas Schmid Date: Mon, 2 Apr 2018 00:09:49 +0200 Subject: [PATCH 2/2] filter synthetic methods for dataprovider tests (#106) * add test cases for junit4 to not execute bridge method as test cases * in addition restructored abstract base class tests a little Signed-off-by: Andreas Schmid --- ...taProviderAbstractAcceptanceBaseTest.java} | 6 +-- .../DataProviderAcceptanceTestInterface.java | 8 ++++ ...ataProviderImplementingAcceptanceTest.java | 42 +++++++++++++++++++ ...DataProviderOverridingAcceptanceTest.java} | 2 +- .../dataprovider/internal/TestGenerator.java | 4 ++ 5 files changed, 58 insertions(+), 4 deletions(-) rename junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/{DataProviderOverrideAcceptanceBaseTest.java => DataProviderAbstractAcceptanceBaseTest.java} (82%) create mode 100644 junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAcceptanceTestInterface.java create mode 100644 junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderImplementingAcceptanceTest.java rename junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/{DataProviderOverrideAcceptanceTest.java => DataProviderOverridingAcceptanceTest.java} (88%) diff --git a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverrideAcceptanceBaseTest.java b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAbstractAcceptanceBaseTest.java similarity index 82% rename from junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverrideAcceptanceBaseTest.java rename to junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAbstractAcceptanceBaseTest.java index 0ab575dc..c1a32e57 100644 --- a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverrideAcceptanceBaseTest.java +++ b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAbstractAcceptanceBaseTest.java @@ -8,7 +8,7 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.UseDataProvider; -public abstract class DataProviderOverrideAcceptanceBaseTest { +public abstract class DataProviderAbstractAcceptanceBaseTest { @DataProvider public static Object[][] dataProviderBase() { return new Object[][] { { "1" } }; @@ -21,9 +21,9 @@ public void testBase(@SuppressWarnings("unused") String one) { } // Does not work since v1.12.0 any more as new dataprovider resolver mechanism uses "Method#getDeclaringClass()" - // instead of "testClass" (= JUnit4 Frameworks "TestClass"); Workaround by specifying "location" explicitely ... + // instead of "testClass" (= JUnit4 Frameworks "TestClass"); Workaround by specifying "location" explicitly ... @Test - @UseDataProvider(location = DataProviderOverrideAcceptanceTest.class) + @UseDataProvider(location = DataProviderOverridingAcceptanceTest.class) public void testChild(String one) { assertThat(one).isEqualTo("1"); } diff --git a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAcceptanceTestInterface.java b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAcceptanceTestInterface.java new file mode 100644 index 00000000..7d76d26a --- /dev/null +++ b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderAcceptanceTestInterface.java @@ -0,0 +1,8 @@ +package com.tngtech.test.java.junit.dataprovider.override; + +public interface DataProviderAcceptanceTestInterface { + + void testToBeImplemented(); + + void testToBeImplemented(T value); +} diff --git a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderImplementingAcceptanceTest.java b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderImplementingAcceptanceTest.java new file mode 100644 index 00000000..e732ccaa --- /dev/null +++ b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderImplementingAcceptanceTest.java @@ -0,0 +1,42 @@ +package com.tngtech.test.java.junit.dataprovider.override; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; + +@RunWith(DataProviderRunner.class) +public class DataProviderImplementingAcceptanceTest implements DataProviderAcceptanceTestInterface { + + private static final AtomicInteger noOfNormalTestsCalls = new AtomicInteger(0); + private static final AtomicInteger noOfDataProviderTestsCalls = new AtomicInteger(0); + + @DataProvider + public static Object[][] dataProviderToBeImplemented() { + // @formatter:off + return new Object[][] { + { 1 }, + { 2 }, + }; + // @formatter:on + } + + @Test + @Override + public void testToBeImplemented() { + assertThat(1).isEqualTo(noOfNormalTestsCalls.incrementAndGet()); + } + + @Test + @UseDataProvider + @Override + public void testToBeImplemented(Integer value) { + assertThat(value).isEqualTo(noOfDataProviderTestsCalls.incrementAndGet()); + } +} diff --git a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverrideAcceptanceTest.java b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverridingAcceptanceTest.java similarity index 88% rename from junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverrideAcceptanceTest.java rename to junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverridingAcceptanceTest.java index 8ff7514d..4941b73b 100644 --- a/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverrideAcceptanceTest.java +++ b/junit4/src/integTest/java/com/tngtech/test/java/junit/dataprovider/override/DataProviderOverridingAcceptanceTest.java @@ -10,7 +10,7 @@ import com.tngtech.java.junit.dataprovider.UseDataProvider; @RunWith(DataProviderRunner.class) -public class DataProviderOverrideAcceptanceTest extends DataProviderOverrideAcceptanceBaseTest { +public class DataProviderOverridingAcceptanceTest extends DataProviderAbstractAcceptanceBaseTest { @Override @Test diff --git a/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/TestGenerator.java b/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/TestGenerator.java index 953d0072..4fc45829 100644 --- a/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/TestGenerator.java +++ b/junit4/src/main/java/com/tngtech/java/junit/dataprovider/internal/TestGenerator.java @@ -49,6 +49,10 @@ public List generateExplodedTestMethodsFor(FrameworkMethod test if (testMethod == null) { return Collections.emptyList(); } + // do not create dataprovider tests for synthetic methods (should normally be handled by JUnit 4 but isn't) + if (testMethod.getMethod().isSynthetic()) { + return Collections.emptyList(); + } if (dataProviderMethod != null) { try { return explodeTestMethod(testMethod, dataProviderMethod);