Lbro
Lbro

Reputation: 361

Quarkus unit tests exclude class

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 ?

Upvotes: 2

Views: 3055

Answers (4)

Balaji G
Balaji G

Reputation: 11

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

Upvotes: 1

gkephorus
gkephorus

Reputation: 1386

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.

Upvotes: 0

Tiago Lopes
Tiago Lopes

Reputation: 1

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.

Upvotes: 0

Lbro
Lbro

Reputation: 361

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 ?

Upvotes: 0

Related Questions