JUnit 5 Extensions
WireMock provides various Extension implementations for JUnit 5 Jupiter. These are manifested as @RegisterExtension
annotated static and instance fields in the test classes.
The plugin provides dedicated actions in the Alt+Insert Generate menu to insert such fields according to the JUnit framework versions suitable for the current test class.
These code insertions are based on dedicated Code Templates, and are applied in the form of on-the-fly created Live Templates.
Generate WireMockExtension field
JUnit 5 works with so-called extensions, for which WireMock has the dedicated com.github.tomakehurst.wiremock.junit5.WireMockExtension
type that can be added as a field, annotated with @org.junit.jupiter.api.extension.RegisterExtension
, in test classes. See WireMock JUnit 5 documentation.
This action is available when only JUnit 5, regardless of the library version, is suitable for the current test class.
@org.junit.jupiter.api.extension.RegisterExtension
static com.github.tomakehurst.wiremock.junit5.WireMockExtension ${NAME} = WireMockExtension.newInstance().options(${OPTIONS}).build();
Template variables:
${NAME}
: Name of the created field. Upon insertion, it suggests potentially suitable field names.${OPTIONS}
: The options configuration with a default value ofcom.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig()
.
Generate WireMockPactExtension field
In case one uses the wiremock-pact extension, an additional action called WireMockPactExtension
is also available that is configured as follows:
@org.junit.jupiter.api.extension.RegisterExtension
static se.bjurr.wiremockpact.wiremockpactextensionjunit5.WireMockPactExtension ${NAME} = new WireMockPactExtension(
WireMockPactConfig.builder()
.setConsumerDefaultValue("${CONSUMER_DEFAULT}")
.setProviderDefaultValue("${PROVIDER_DEFAULT}"));
Template variables:
${NAME}
: Name of the created field. Upon insertion, it suggests potentially suitable field names.${CONSUMER_DEFAULT}
: The default consumer name.${PROVIDER_DEFAULT}
: The default provider name.${PACT_JSON_FOLDER}
: The Pact JSON folder. It may be used for configuring the argument ofWireMockPactConfig.setPactJsonFolder()
.
Convert test classes between the declarative and programmatic approaches
For JUnit 5 WireMock has a declarative and a programmatic mode for running one or more WireMock instances.
From declarative to programmatic
This intention can convert the declarative variant to the programmatic one. It is available on @WireMockTest
annotated Java classes when there is no WireMockExtension
field defined in the class, and it performs the following changes:
- creates a
WireMockExtension
field (calledwm
) configured based on the attribute values in the@WireMockTest
annotation, - replaces each occurrence of
WireMock.stubFor()
withwm.stubFor()
in the test class, so that the new field is referenced in the test code, - removes the
@WireMockTest
annotation from the class.
The configuration of the field happens as follows:
Case | From | To |
---|---|---|
Default configuration | @WireMocktest | @RegisterExtension |
httpPort | @WireMocktest(httpPort = 1234) | @RegisterExtension .options(WireMockConfiguration.wireMockConfig().port(1234)) .build(); |
httpsEnabled | @WireMocktest(httpsEnabled = true) | @RegisterExtension |
httpsPort | @WireMocktest(httpsPort = 1234) | Since httpsEnabled is false, httpsPort is not set at all:@RegisterExtension |
httpsEnabled + httpsPort | @WireMocktest(httpsEnabled = true, httpsPort = 1234) | @RegisterExtension |
proxyMode | @WireMocktest(proxyMode = true) | @RegisterExtension |
extensionScanningEnabled | @WireMocktest(extensionScanningEnabled = true) | @RegisterExtension |
From programmatic to declarative
This intention can convert from the programmatic variant to the declarative one, and is available
- on Java classes not annotated with
@WireMockTest
(not even any of their base classes), - when there is only one
WireMockExtension
field defined in the class, - and when there is no call on
WireMockExtension.Builder
orWireMockConfiguration
that is not supported by the@WireMockTest
annotation.
It performs the following changes:
- Creates a
@WireMockTest
annotation configured based on theWireMockExtension
field’s initializer, and adds that annotation to the target class. - Replaces each occurrence of
wm.stubFor()
(wm
is the name of the source field) withWireMock.stubFor()
, so that the field references are removed. - Removes the source
WireMockExtension
field from the class.
The configuration of the annotation happens as follows:
Case | From | To |
---|---|---|
Default configuration | @RegisterExtension | @WireMockTest |
httpPort | @RegisterExtension | WireMockTest(httpPort = 8090) |
@RegisterExtension | WireMockTest(httpPort = 8090) | |
Dynamic httpPort | @RegisterExtension | @WireMockTest |
proxyMode enabled | @RegisterExtension | @WireMockTest(proxyMode = true) |
proxyMode disabled | @RegisterExtension | @WireMockTest |
Dynamic httpsPort | @RegisterExtension | @WireMockTest(httpsEnabled = true) |
httpsPort | @RegisterExtension | @WireMockTest(httpsEnabled = true, httpsPort = 8090) |
@RegisterExtension | @WireMockTest(httpsEnabled = true, httpsPort = 8090) | |
extensionScanningEnabled | @RegisterExtension | @WireMocktest(extensionScanningEnabled = true) |
@WireMockTest annotation
Inlay hints
Based on WireMock’s JUnit5 usage documentation, when either the httpPort
or the httpsPort
attribute is not specified, random ports are used.
To inform users about this, an inlay hint is displayed on top of the @WireMockTest
annotation if either one of those attributes is not specified. In any case other than the ones below, no hint is displayed.
httpPort specified | httpsEnabled | httpsPort specified | Inlay hint text |
---|---|---|---|
false | false/true | false/true | dynamic HTTP port |
false | true | false | dynamic HTTP and HTTPS ports |
true | true | false | dynamic HTTPS port |
Inspections
This inspection reports issues regarding the configuration of the @WireMockTest
annotation.
Currently, it reports the httpsPort
attribute if the httpsEnabled
attribute is set to false
either explicitly or by default, because the HTTPS port number takes effect only if HTTPS is enabled.
Since v1.0.1 a quick fix is also available to set the httpsEnabled
attribute to true, thus enabling HTTPS.
from: @WireMockTest(httpsEnabled = false, httpsPort = 2345)
to: @WireMockTest(httpsEnabled = true, httpsPort = 2345)
from: @WireMockTest(httpsPort = 2345)
to: @WireMockTest(httpsPort = 2345, httpsEnabled = true)