From 0984e5a4fb873e86cff2ac25b0878676d758514b Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 11 Oct 2022 08:43:11 +0300 Subject: [PATCH] Allow mixing @QuarkusTest and @QuarkusMainTest This is done by "restarting" the application when one test type follows the other. Fixes: #28486 --- .../test/junit/QuarkusMainTestExtension.java | 13 +++++++++---- .../io/quarkus/test/junit/QuarkusTestExtension.java | 13 ++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java index 95c90923c412e..aeb551a1cd7b7 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java @@ -64,13 +64,18 @@ private void ensurePrepared(ExtensionContext extensionContext, Class testType = store.get(IO_QUARKUS_TESTING_TYPE, Class.class); if (testType != null) { if (testType != QuarkusMainTest.class) { - throw new IllegalStateException( - "Cannot mix both @QuarkusMainTest based tests and " + testType.getName() - + " based tests in the same run"); + if (testType.equals(QuarkusTest.class)) { + mixedWithQuarkusTest = true; + } else { + throw new IllegalStateException( + "Cannot mix both @QuarkusMainTest based tests and " + testType.getName() + + " based tests in the same run"); + } } } else { store.put(IO_QUARKUS_TESTING_TYPE, QuarkusMainTest.class); @@ -78,7 +83,7 @@ private void ensurePrepared(ExtensionContext extensionContext, Class clazz) { } private QuarkusTestExtensionState ensureStarted(ExtensionContext extensionContext) { + boolean mixedWithQuarkusMainTest = false; ExtensionContext.Store store = getStoreFromContext(extensionContext); Class testType = store.get(IO_QUARKUS_TESTING_TYPE, Class.class); if (testType != null) { if (testType != QuarkusTest.class) { - throw new IllegalStateException( - "Cannot mix both @QuarkusTest based tests and " + testType.getName() + " based tests in the same run"); + if (testType.equals(QuarkusMainTest.class)) { + mixedWithQuarkusMainTest = true; + } else { + throw new IllegalStateException( + "Cannot mix both @QuarkusTest based tests and " + testType.getName() + + " based tests in the same run"); + } } } else { store.put(IO_QUARKUS_TESTING_TYPE, QuarkusTest.class); } QuarkusTestExtensionState state = getState(extensionContext); Class selectedProfile = getQuarkusTestProfile(extensionContext); - boolean wrongProfile = !Objects.equals(selectedProfile, quarkusTestProfile); + boolean wrongProfile = !Objects.equals(selectedProfile, quarkusTestProfile) || mixedWithQuarkusMainTest; // we reload the test resources if we changed test class and the new test class is not a nested class, and if we had or will have per-test test resources boolean reloadTestResources = !Objects.equals(extensionContext.getRequiredTestClass(), currentJUnitTestClass) && !isNested(currentJUnitTestClass, extensionContext.getRequiredTestClass())