You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2020/11/04 16:43:27 UTC

[camel-quarkus] 01/07: Fix WireMock server field injection for native tests

This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git

commit 98c2f396e522fa1a770c01bf72737db7c105afe6
Author: James Netherton <ja...@gmail.com>
AuthorDate: Wed Nov 4 10:17:20 2020 +0000

    Fix WireMock server field injection for native tests
---
 .../WireMockTestResourceLifecycleManager.java      | 36 ++++++++++++----------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/integration-tests-support/wiremock/src/main/java/org/apache/camel/quarkus/test/wiremock/WireMockTestResourceLifecycleManager.java b/integration-tests-support/wiremock/src/main/java/org/apache/camel/quarkus/test/wiremock/WireMockTestResourceLifecycleManager.java
index dc64c87..5308534 100644
--- a/integration-tests-support/wiremock/src/main/java/org/apache/camel/quarkus/test/wiremock/WireMockTestResourceLifecycleManager.java
+++ b/integration-tests-support/wiremock/src/main/java/org/apache/camel/quarkus/test/wiremock/WireMockTestResourceLifecycleManager.java
@@ -116,26 +116,30 @@ public abstract class WireMockTestResourceLifecycleManager implements QuarkusTes
     @Override
     public void inject(Object testInstance) {
         if (isMockingEnabled() || isRecordingEnabled()) {
-            Class<?> c = testInstance.getClass();
-            for (Field field : c.getDeclaredFields()) {
-                if (field.getAnnotation(MockServer.class) != null) {
-                    if (!WireMockServer.class.isAssignableFrom(field.getType())) {
-                        throw new RuntimeException("@MockServer can only be used on fields of type WireMockServer");
-                    }
+            Class<?> testClass = testInstance.getClass();
+            while (testClass != Object.class) {
+                for (Field field : testClass.getDeclaredFields()) {
+                    if (field.getAnnotation(MockServer.class) != null) {
+                        if (!WireMockServer.class.isAssignableFrom(field.getType())) {
+                            throw new RuntimeException("@MockServer can only be used on fields of type WireMockServer");
+                        }
 
-                    field.setAccessible(true);
-                    try {
-                        if (server == null) {
-                            server = createServer();
-                            server.start();
+                        field.setAccessible(true);
+                        try {
+                            if (server == null) {
+                                LOG.info("Starting WireMockServer");
+                                server = createServer();
+                                server.start();
+                            }
+                            LOG.infof("Injecting WireMockServer for field %s", field.getName());
+                            field.set(testInstance, server);
+                            return;
+                        } catch (Exception e) {
+                            throw new RuntimeException(e);
                         }
-                        LOG.infof("Injecting WireMockServer for field %s", field.getName());
-                        field.set(testInstance, server);
-                        return;
-                    } catch (Exception e) {
-                        throw new RuntimeException(e);
                     }
                 }
+                testClass = testClass.getSuperclass();
             }
         }
     }