diff --git a/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java b/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java index ba5e42bf..139f7a98 100644 --- a/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java +++ b/extension-testcontainer/src/main/java/io.camunda.zeebe.process.test.extension.testcontainer/ZeebeProcessTestExtension.java @@ -123,14 +123,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable private void injectFields(final ExtensionContext extensionContext, final Object... objects) { final Class requiredTestClass = extensionContext.getRequiredTestClass(); - final Field[] declaredFields = requiredTestClass.getDeclaredFields(); for (final Object object : objects) { - final Optional field = getField(declaredFields, object); + final Optional field = getField(requiredTestClass, object); field.ifPresent(value -> injectField(extensionContext, value, object)); } } - private Optional getField(final Field[] declaredFields, final Object object) { + private Optional getField(final Class requiredTestClass, final Object object) { + final Field[] declaredFields = requiredTestClass.getDeclaredFields(); + final List fields = Arrays.stream(declaredFields) .filter(field -> field.getType().isInstance(object)) @@ -143,8 +144,12 @@ private Optional getField(final Field[] declaredFields, final Object obje + "found %s. Please make sure at most one field of type %s has been declared in the" + " test class.", object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName())); + } else if (fields.size() == 0) { + final Class superclass = requiredTestClass.getSuperclass(); + return superclass == null ? Optional.empty() : getField(superclass, object); + } else { + return Optional.of(fields.get(0)); } - return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0)); } private void injectField( diff --git a/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java b/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java index edea3dbb..41b59fe5 100644 --- a/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java +++ b/extension/src/main/java/io/camunda/zeebe/process/test/extension/ZeebeProcessTestExtension.java @@ -99,14 +99,15 @@ public void testFailed(final ExtensionContext extensionContext, final Throwable private void injectFields(final ExtensionContext extensionContext, final Object... objects) { final Class requiredTestClass = extensionContext.getRequiredTestClass(); - final Field[] declaredFields = requiredTestClass.getDeclaredFields(); for (final Object object : objects) { - final Optional field = getField(declaredFields, object); + final Optional field = getField(requiredTestClass, object); field.ifPresent(value -> injectField(extensionContext, value, object)); } } - private Optional getField(final Field[] declaredFields, final Object object) { + private Optional getField(final Class requiredTestClass, final Object object) { + final Field[] declaredFields = requiredTestClass.getDeclaredFields(); + final List fields = Arrays.stream(declaredFields) .filter(field -> field.getType().isInstance(object)) @@ -119,8 +120,12 @@ private Optional getField(final Field[] declaredFields, final Object obje + "found %s. Please make sure at most one field of type %s has been declared in the" + " test class.", object.getClass().getSimpleName(), fields.size(), object.getClass().getSimpleName())); + } else if (fields.size() == 0) { + final Class superclass = requiredTestClass.getSuperclass(); + return superclass == null ? Optional.empty() : getField(superclass, object); + } else { + return Optional.of(fields.get(0)); } - return fields.size() == 0 ? Optional.empty() : Optional.of(fields.get(0)); } private void injectField( diff --git a/qa/abstracts/src/main/java/io/camunda/zeebe/process/test/qa/abstracts/injection/AbstractInheritanceInjectionTest.java b/qa/abstracts/src/main/java/io/camunda/zeebe/process/test/qa/abstracts/injection/AbstractInheritanceInjectionTest.java new file mode 100644 index 00000000..f6453d7d --- /dev/null +++ b/qa/abstracts/src/main/java/io/camunda/zeebe/process/test/qa/abstracts/injection/AbstractInheritanceInjectionTest.java @@ -0,0 +1,34 @@ +/* + * Copyright © 2021 camunda services GmbH (info@camunda.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.camunda.zeebe.process.test.qa.abstracts.injection; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.camunda.zeebe.client.ZeebeClient; +import io.camunda.zeebe.process.test.api.ZeebeTestEngine; +import org.junit.jupiter.api.Test; + +public abstract class AbstractInheritanceInjectionTest { + protected ZeebeClient client; + protected ZeebeTestEngine engine; + + @Test + void testFieldsAreInjectedSuccessfully() { + assertThat(client).isNotNull(); + assertThat(engine).isNotNull(); + } +} diff --git a/qa/embedded/src/test/java/io/camunda/zeebe/process/test/qa/embedded/injection/InheritanceInjectionTest.java b/qa/embedded/src/test/java/io/camunda/zeebe/process/test/qa/embedded/injection/InheritanceInjectionTest.java new file mode 100644 index 00000000..d329155c --- /dev/null +++ b/qa/embedded/src/test/java/io/camunda/zeebe/process/test/qa/embedded/injection/InheritanceInjectionTest.java @@ -0,0 +1,23 @@ +/* + * Copyright © 2021 camunda services GmbH (info@camunda.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.camunda.zeebe.process.test.qa.embedded.injection; + +import io.camunda.zeebe.process.test.extension.ZeebeProcessTest; +import io.camunda.zeebe.process.test.qa.abstracts.injection.AbstractInheritanceInjectionTest; + +@ZeebeProcessTest +class InheritanceInjectionTest extends AbstractInheritanceInjectionTest {} diff --git a/qa/testcontainers/src/test/java/io/camunda/zeebe/process/test/qa/testcontainer/injection/InheritanceInjectionTest.java b/qa/testcontainers/src/test/java/io/camunda/zeebe/process/test/qa/testcontainer/injection/InheritanceInjectionTest.java new file mode 100644 index 00000000..aceef980 --- /dev/null +++ b/qa/testcontainers/src/test/java/io/camunda/zeebe/process/test/qa/testcontainer/injection/InheritanceInjectionTest.java @@ -0,0 +1,23 @@ +/* + * Copyright © 2021 camunda services GmbH (info@camunda.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.camunda.zeebe.process.test.qa.testcontainer.injection; + +import io.camunda.zeebe.process.test.extension.testcontainer.ZeebeProcessTest; +import io.camunda.zeebe.process.test.qa.abstracts.injection.AbstractInheritanceInjectionTest; + +@ZeebeProcessTest +class InheritanceInjectionTest extends AbstractInheritanceInjectionTest {}