Skip to content

Commit 9d85d67

Browse files
committed
Late init for better unit tests
1 parent 9f752b1 commit 9d85d67

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

docs/asciidoc/testing.adoc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,15 @@ class TestApp {
242242
For MVC routes you might prefer to write a unit test using a mock library. No need to use
243243
`MockRouter`, but it is possible too.
244244

245-
By default the javadoc:MockRouter[] class execute the route handler. For executing the entire
246-
pipeline use: javadoc:MockRouter[setFullExecution].
245+
==== Options
247246

248-
Unit testing is simple and easy in Jooby. The javadoc:MockRouter[] let you to execute route function,
247+
- javadoc:MockRouter[setFullExecution]: the javadoc:MockRouter[] class ONLY execute the route
248+
handler. For executing the entire pipeline use: javadoc:MockRouter[setFullExecution].
249+
250+
- javadoc:Jooby[setLateInit]: extension modules usually run at the time they are installed it.
251+
This might not be ideally for unit tests. To delay extension initialization use the javadoc:Jooby[setLateInit] mode.
252+
253+
Unit testing is simple and easy in Jooby. The javadoc:MockRouter[] let you execute the route function,
249254
while the javadoc:MockContext[] allows you to create an light-weight and mutable context where
250255
you can set HTTP parameters, body, headers, etc.
251256

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public class Jooby implements Router, Registry {
9292

9393
private EnvironmentOptions environmentOptions;
9494

95+
private boolean lateInit;
96+
9597
/**
9698
* Creates a new Jooby instance.
9799
*/
@@ -310,7 +312,7 @@ public Jooby renderer(@Nonnull MediaType contentType, @Nonnull Renderer renderer
310312
* @return This application.
311313
*/
312314
@Nonnull public Jooby install(@Nonnull Extension extension) {
313-
if (extension.lateinit()) {
315+
if (lateInit || extension.lateinit()) {
314316
onStarting(() -> extension.install(this));
315317
} else {
316318
try {
@@ -477,13 +479,6 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
477479
return this.router.getServices();
478480
}
479481

480-
private Registry checkRegistry() {
481-
if (registry == null) {
482-
throw new IllegalStateException("No registry available");
483-
}
484-
return registry;
485-
}
486-
487482
/**
488483
* Get base application package. This is the package from where application was initialized
489484
* or the package of a Jooby application sub-class.
@@ -627,6 +622,21 @@ private Registry checkRegistry() {
627622
return this;
628623
}
629624

625+
/**
626+
* Force all module to be initialized lazily at application startup time (not at
627+
* creation/instantiation time).
628+
*
629+
* This option is present mostly for unit-test where you need to instantiated a Jooby instance
630+
* without running extensions.
631+
*
632+
* @param lateInit True for late init.
633+
* @return This application.
634+
*/
635+
public Jooby setLateInit(boolean lateInit) {
636+
this.lateInit = lateInit;
637+
return this;
638+
}
639+
630640
@Override public String toString() {
631641
return router.toString();
632642
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.jooby;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.concurrent.atomic.AtomicBoolean;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class LateInitTest {
10+
11+
@Test
12+
public void defaultLateInit() {
13+
Jooby app = new Jooby();
14+
AtomicBoolean run = new AtomicBoolean();
15+
app.install(router-> run.set(true));
16+
assertEquals(true, run.get());
17+
}
18+
19+
@Test
20+
public void lateInitOn() {
21+
Jooby app = new Jooby();
22+
AtomicBoolean run = new AtomicBoolean();
23+
app.setLateInit(true);
24+
app.install(router-> run.set(true));
25+
assertEquals(false, run.get());
26+
}
27+
28+
}

modules/jooby-test/src/test/java/io/jooby/UnitTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import io.reactivex.Single;
44
import org.junit.jupiter.api.Test;
55

6+
import java.util.concurrent.atomic.AtomicBoolean;
7+
68
import static io.jooby.StatusCode.NO_CONTENT;
79
import static org.junit.jupiter.api.Assertions.assertEquals;
810
import static org.mockito.Mockito.mock;
@@ -116,4 +118,5 @@ public void mvcApp() {
116118
MockRouter router = new MockRouter(new MvcApp());
117119
assertEquals("/mvc", router.get("/mvc").value());
118120
}
121+
119122
}

0 commit comments

Comments
 (0)