Skip to content

Commit

Permalink
add lambda-based listener methods for TestEnvironmentSetup (alternati…
Browse files Browse the repository at this point in the history
…ve to listen method);

move helper test objects lookup methods from listener interface into EventContext object (passed into listener) for lambda listeners usage simplification
  • Loading branch information
xvik committed Feb 20, 2025
1 parent 42cfaf8 commit 39e59c7
Show file tree
Hide file tree
Showing 20 changed files with 676 additions and 307 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
(useful when TestInstance.Lifecycle.PER_CLASS used) (discussion #394)
- Add getJunitContext() method for TestEnvironmentSetup (@EnableSetup) to be able to configure
test application with full context access (a setup object could register hooks) (discussion #388)
- Add test lifecycle listeners: could be registered with TestEnvironmentSetup (listen() method) and provide
notifications for guicey extension lifecycle (app start/stop, before/after test).
- Add test lifecycle listeners: could be registered with TestEnvironmentSetup (listen() method or lambda-based on* methods)
and provide notifications for guicey extension lifecycle (app start/stop, before/after test).
This is a simple alternative to writing junit extensions for an additional integrations (db, testcontainers etc.).
- Add annotated fields search api in test class for setup objects (TestEnvironmentSetup): findFields(..)
(to simplify writing annotation-driven extensions).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* method), otherwise it must be static. Incorrect usage will be indicated with an exception.
* <p>
* If setup object implements hook ({@link ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook})
* and/or listener ({@link ru.vyarus.dropwizard.guice.test.jupiter.env.TestExecutionListener}) it would be
* and/or listener ({@link ru.vyarus.dropwizard.guice.test.jupiter.env.listen.TestExecutionListener}) it would be
* registered automatically (no need for manual registration). Manual registration would not create duplicate.
*
* @author Vyacheslav Rusakov
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
import org.junit.jupiter.api.extension.ExtensionContext;
import ru.vyarus.dropwizard.guice.test.jupiter.env.listen.EventContext;
import ru.vyarus.dropwizard.guice.test.jupiter.env.listen.TestExecutionListener;
import ru.vyarus.dropwizard.guice.test.jupiter.ext.conf.track.GuiceyTestTime;
import ru.vyarus.dropwizard.guice.test.jupiter.ext.conf.track.TestExtensionsTracker;

Expand Down Expand Up @@ -30,27 +32,27 @@ public void addListener(final TestExecutionListener listener) {
}

public void broadcastStart(final ExtensionContext context) {
broadcast(listener -> listener.started(context), false);
broadcast(listener -> listener.started(new EventContext(context)), false);
}

public void broadcastBeforeAll(final ExtensionContext context) {
broadcast(listener -> listener.beforeAll(context), true);
broadcast(listener -> listener.beforeAll(new EventContext(context)), true);
}

public void broadcastBefore(final ExtensionContext context) {
broadcast(listener -> listener.beforeEach(context), true);
broadcast(listener -> listener.beforeEach(new EventContext(context)), true);
}

public void broadcastAfter(final ExtensionContext context) {
broadcast(listener -> listener.afterEach(context), true);
broadcast(listener -> listener.afterEach(new EventContext(context)), true);
}

public void broadcastAfterAll(final ExtensionContext context) {
broadcast(listener -> listener.afterAll(context), true);
broadcast(listener -> listener.afterAll(new EventContext(context)), true);
}

public void broadcastStop(final ExtensionContext context) {
broadcast(listener -> listener.stopped(context), false);
broadcast(listener -> listener.stopped(new EventContext(context)), false);
}

private void broadcast(final Consumer<TestExecutionListener> action, final boolean append) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
* closed automatically.
* <p>
* If auto close is not enough, use
* {@link ru.vyarus.dropwizard.guice.test.jupiter.env.TestExtension#listen(TestExecutionListener)} listener for
* reacting on exact test phases.
* {@link ru.vyarus.dropwizard.guice.test.jupiter.env.TestExtension#listen(
* ru.vyarus.dropwizard.guice.test.jupiter.env.listen.TestExecutionListener)} listener for reacting on exact test
* phases (or lambda-based listener methods: on*).
* <p>
* The same could be achieved with an additional junit 5 extensions, but it might be harder to properly synchronize
* lifecycles (extensions order would be important). Environment support assumed to be a simpler alternative.
Expand All @@ -31,7 +32,7 @@
* (inside it) providing entire junit context or just some stored values.
* <p>
* AUTO REGISTRATION: If setup object implements hook ({@link ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook})
* and/or listener ({@link ru.vyarus.dropwizard.guice.test.jupiter.env.TestExecutionListener}) it would be
* and/or listener ({@link ru.vyarus.dropwizard.guice.test.jupiter.env.listen.TestExecutionListener}) it would be
* registered automatically. Manual registration would not create duplicate.
*
* @author Vyacheslav Rusakov
Expand Down

This file was deleted.

Loading

0 comments on commit 39e59c7

Please sign in to comment.