Skip to content

Commit 6324903

Browse files
committed
update docs
1 parent d3402da commit 6324903

File tree

2 files changed

+107
-27
lines changed

2 files changed

+107
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
### 2.1.0 (2026-02-17)
12
* Add @DebugJunitExtensions annotation to print registered and called junit extensions
23
* Add @IgnoreJunitExtensions annotation to ignore some declared junit extensions
34
(to resolve collisions like with spock-spring)
4-
* Automatically ignore junit SpringExtension if spock-spring is present
5-
(spock manage spring context and all other junit extensions will work)
5+
* spock-spring compatibility: junit SpringExtension automatically ignored
6+
(spock manages spring context and all other junit extensions will work)
67

78
### 2.0.1 (2026-02-06)
89
* Fix github repository links in POM

README.md

Lines changed: 104 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ validated with tests.
4444
Originally developed for [dropwizard-guicey](https://github.com/xvik/dropwizard-guicey) to avoid maintaining special
4545
spock extensions.
4646

47-
Spock 1 provides spock-junit4 module to support junit 4 rules. At that time spock extensions model was "light years"
48-
ahead
47+
Spock 1 provides spock-junit4 module to support junit 4 rules. At that time spock extensions model was "light years" ahead
4948
of junit. But junit 5 extensions are nearly equal in power to spock extensions (both has pros and cons). It is a big
5049
loss for spock to ignore junit5 extensions:
5150
junit extensions are easier to write, but spock is still much better for writing tests.
@@ -67,14 +66,14 @@ Maven:
6766
<dependency>
6867
<groupId>ru.vyarus</groupId>
6968
<artifactId>spock-junit6</artifactId>
70-
<version>2.0.1</version>
69+
<version>2.1.0</version>
7170
</dependency>
7271
```
7372

7473
Gradle:
7574

7675
```groovy
77-
implementation 'ru.vyarus:spock-junit6:2.0.1'
76+
implementation 'ru.vyarus:spock-junit6:2.1.0'
7877
```
7978

8079
**For junit 5 setup** see [junit5 docs](https://github.com/xvik/spock-junit5/tree/junit5).
@@ -89,12 +88,12 @@ and to prevent usage with lower junit versions.
8988

9089
There is a high chance that a more recent module version will work with older junit versions
9190
(it depends on what extensions are used).
92-
In case of problems (like `NoClassDefFoundError`) select a lower module version (according to your junit version)
91+
In case of problems (like `NoClassDefFoundError`) select a lower module version (according to your junit version)
9392

9493
| Junit | Artifact | Version | Junit API Changes
9594
|-------------|-------------------------|----------------------------------------------------------|--------------------------------------------------------------
96-
| 6.0 | **spock&#x2011;junit6** | [2.0.1](https://github.com/xvik/spock-junit5/tree/2.0.1) | value storage API changes, java 17 required
97-
| 5.12 - 5.14 | spock&#x2011;junit5 | [1.4.1](https://github.com/xvik/spock-junit5/tree/1.4.1) | new methods in ExtensionContext
95+
| 6.0 | **spock&#x2011;junit6** | [2.1.0](https://github.com/xvik/spock-junit5/tree/2.1.0) | value storage API changes, java 17 required
96+
| 5.12 - 5.14 | spock&#x2011;junit5 | [1.5.0](https://github.com/xvik/spock-junit5/tree/1.5.0) | new methods in ExtensionContext
9897
| 5.11 | spock&#x2011;junit5 | [1.3.0](https://github.com/xvik/spock-junit5/tree/1.3.0) | changed initialization order for non-static extension fields
9998
| 5.9 - 5.10 | spock&#x2011;junit5 | [1.2.0](https://github.com/xvik/spock-junit5/tree/1.2.0) |
10099
| 5.7 - 5.8 | spock&#x2011;junit5 | [1.0.1](https://github.com/xvik/spock-junit5/tree/1.0.1) |
@@ -307,30 +306,113 @@ Extensions like `TestInfoParameterResolver` could be easully enabled with `@Exte
307306

308307
So overall not automatic defaults should not be a problem.
309308

309+
### Debugging
310+
311+
Annotate spock test with `@DebugJunitExtensions` and all registered junit extensions will be printed to console:
312+
313+
```groovy
314+
@ExtendWith([Ext0, Ext1])
315+
@DebugJunitExtensions
316+
class Test extends Specification {
317+
```
318+
319+
```
320+
[junit] Registered test class (Test) extensions: Ext0, Ext1
321+
[junit] BeforeAllCallback extensions called: Ext0
322+
[junit] BeforeTestExecutionCallback extensions called: Ext1
323+
```
324+
325+
Optionally, extension registrations or extension execution messages could be disabled:
326+
327+
```groovy
328+
@DebugJunitExtensions(registrations = false)
329+
```
330+
331+
```groovy
332+
@DebugJunitExtensions(usage = false)
333+
```
334+
335+
If you have many extensions, it might be easier to read in column mode:
336+
337+
```groovy
338+
@DebugJunitExtensions(columnMode = true)
339+
```
340+
341+
```groovy
342+
[junit] Registered test class (Test) extensions:
343+
Ext0
344+
Ext1
345+
```
346+
347+
### Ignore extensions
348+
349+
Sometimes it is useful to ignore some junit extensions: for example, if spock extension
350+
already uses extension annotation natively (like `@SpringBootTest` used by spock-spring module).
351+
352+
Use `@IgnoreJunitExtensions` annotation to ignore any junit extensions:
353+
354+
```groovy
355+
@ExtendWith([Ext0, Ext1])
356+
@IgnoreJunitExtensions(Ext1)
357+
class Test extends Specification {
358+
```
359+
360+
NOTE: this could be used to temporarily disable some extensions (without removing their declaration)
361+
362+
Use `@DebugJunitExtensions` to see which junit extensions are actually used
363+
(there are no special messages about ignored extensions)
364+
310365
### Usage with Spring-Boot
311366

312-
Only spock and spock-junit5 dependencies would be required:
367+
Spock-junit6 could be used either with `spock-spring` or with `spock-core`.
368+
369+
Note: for spring-boot 3 use [spock-junit5](https://github.com/xvik/spock-junit5/tree/junit5)
370+
371+
#### Spock-spring compatibility
313372

314-
**Spring-boot 4**:
373+
When `spock-spring` is in the classpath:
315374

316375
```groovy
317-
testImplementation 'org.spockframework:spock-core:2.4-groovy-5.0'
318-
testImplementation 'ru.vyarus:spock-junit6:2.0.1'
376+
testImplementation 'org.spockframework:spock-spring:2.4-groovy-5.0'
377+
testImplementation 'ru.vyarus:spock-junit6:2.1.0'
319378
```
320379

321-
**Spring-boot 3**:
380+
spock-junit6 **automatically disables** junit `SpringExtension`.
381+
This means that `spock-spring` will startup spring context (not junit extension!),
382+
but all other junit extensions will work (essentially, it "applies" `@IngoreJunitExtensions(SpringExtension)` automatically).
383+
384+
There would be a new message in log, indicating that you're using pure spock spring support (and an alternative is possible):
385+
386+
```
387+
[spock-junit6] Spring junit extension disabled (org.springframework.test.context.junit.jupiter.SpringExtension) because spock-spring detected and so it will handle spring context initialization (all other junit extensions will work). If you want to run spring as junit extension, use spock-core only (without spock-spring).
388+
```
389+
390+
The main benefit is that you **could use** spock mocking (`@SpringBean`) and other spock features.
391+
392+
IMPORTANT: In case of problems (spring has many test annotations, and I did not check all of them),
393+
use `@DebugJunitExtensions` to see what extensions are actually used and `@IgnoreJunitExtensions` to ignore
394+
conflicting extensions (assuming spock-spring already implements them natively).
395+
Or just migrate to `spock-core` and use pure junit spring extensions (see below).
396+
397+
398+
#### Pure junit way
399+
400+
If you want to use the native junit spring extension, then don't apply `spock-spring` (use `spock-core` only):
322401

323402
```groovy
324-
testImplementation 'org.spockframework:spock-core:2.4-groovy-4.0'
325-
testImplementation 'ru.vyarus:spock-junit5:1.4.1'
403+
testImplementation 'org.spockframework:spock-core:2.4-groovy-5.0'
404+
testImplementation 'ru.vyarus:spock-junit6:2.1.0'
326405
```
327406

328-
Note that [spock-spring module](https://spockframework.org/spock/docs/2.3/modules.html#_spring_module)
329-
**should not be used** together with spock-junit5 for spring-boot tests!
330-
Spock-junit5 would activate native spring junit5 extensions **the same way** as in raw junit
407+
In this case, the native junit spring extension will be used.
408+
409+
NOTE: in this case, you **would not** be able to use spock mocking (`@SpringBean`), instead you'll have
410+
to use [mockito alternatives](https://docs.spring.io/spring-boot/reference/testing/spring-boot-applications.html#testing.spring-boot-applications.mocking-beans)
411+
(`@MockitoBean` and `@MockitoSpyBean`).
331412

332-
Example MVC test (based
333-
on [this example](https://github.com/mkyong/spring-boot/blob/master/spring-boot-hello-world/src/test/java/com/mkyong/HelloControllerTests.java)):
413+
#### MVC test example
414+
415+
Example MVC test (based on [this example](https://github.com/mkyong/spring-boot/blob/master/spring-boot-hello-world/src/test/java/com/mkyong/HelloControllerTests.java)):
334416

335417
```groovy
336418
@SpringBootTest
@@ -353,13 +435,12 @@ class ControllerTest extends Specification {
353435
}
354436
```
355437

356-
Example JPA test (based
357-
on [this example](https://github.com/mkyong/spring-boot/blob/master/spring-data-jpa/src/test/java/com/mkyong/BookRepositoryTest.java)):
438+
Example JPA test (based on [this example](https://github.com/mkyong/spring-boot/blob/master/spring-data-jpa/src/test/java/com/mkyong/BookRepositoryTest.java)):
358439

359440
```groovy
360441
@DataJpaTest
361442
class BootTest extends Specification {
362-
443+
363444
@Autowired
364445
TestEntityManager testEM
365446
@Autowired
@@ -461,9 +542,7 @@ class SpockExtensionImpl implements IAnnotationDrivenExtension<SpockExtension> {
461542
spec.allFixtureMethods*.addInterceptor new I('fixture method')
462543
}
463544
464-
static class I implements IMethodInterceptor {
465-
...
466-
}
545+
static class I implements IMethodInterceptor { ... }
467546
}
468547
```
469548

0 commit comments

Comments
 (0)