2

I am moving from Thorntail to Quarkus. In my tests I used to create a @deployment method in which I put only what was needed by the tests. In particular I didn't put a Class having a @Startup annotation (because I didn't want to test that ...). When I moved to QUARKUS, I suppress de @deployment static method, then when I launch the tests @Startup is ... started and a lot of bad things happen which prevent me from testing what I want to test (well, it crashes because it tries to connect to services which are not available).

So the question is : is there a way to exclude some package or class when lauching a test with quarkusTest ?

Lbro
  • 309
  • 2
  • 16

4 Answers4

1

quarkus.arc.test.disable-application-lifecycle-observers=true also seems to be effective if you want to exclude the app life cycle observers.

0

I finally created a class :

@ApplicationScoped
public class ApplicationLifeCycle {
private final Logger log = Logger.getLogger(getClass());

@Inject
Startup startup;

void onStart(@Observes StartupEvent ev) {
    log.info("The application is starting with profile " + ProfileManager.getActiveProfile());
    if (!ProfileManager.getActiveProfile().equalsIgnoreCase("test")) {
        startup.getModel();
    }
}

void onStop(@Observes ShutdownEvent ev) {
    log.info("The application is stopping...");
    startup.stopMQ();
}
}

A bit ugly isn't it ? Is there a better way to do it ?

Lbro
  • 309
  • 2
  • 16
  • I'm new to Quarkus and I have exactly the same problem.. I want to test the service layer connecting to some third party services which I don't want to be run within test. I mock and annotate this service as `@InjectMock`, but it seems that `@Observes StartupEvent` is still triggered in the "concrete" instance of the service - of course causing the service to connect to this third party services. I'm about to do similar solution as you did, but I hate to have additional code in order for tests to work. – NejcT Oct 02 '20 at 08:57
0

Quarkus adds a few capabilities to handle such scenarios regarding CDI, which can be found here: https://quarkus.io/guides/cdi-reference#enable_build_profile

For instance:

@ApplicationScoped
@UnlessBuildProfile("test")
class ConnectsToExternalService implements ExternalConnector {}

Will prevent CDI from injecting this implementation when the quarkus profile is "test", which quarkus sets when running a @QuarkusTest.

So if you inject "ExternalConnector" in a @QuarkusTest, you'll get an Unsatisfied dependency exception, which can be fixed with:

@ApplicationScoped
@IfBuildProfile("test") // not required if class is in "test" dir 
class MockExternalService implements ExternalConnector {}

Using this approach you can prevent external connections but also mock them.

0

To top things off, one can now disable all StartupEvent and ShutdownEvent observers declared on application using a QuarkusTestProfile in combination with @TestProfile (See Quarkus Testing with Different Profiles). You have to return true in disableApplicationLifecycleObservers in that case.

gkephorus
  • 1,232
  • 18
  • 31