JUnit 4 Rules
WireMock provides various Rule implementations for JUnit 4 and JUnit 5 Vintage. These are manifested as @Rule
and @ClassRule
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 Rule and ClassRule fields
In case of class rules, the official WireMock documentation and WireMock itself differentiates and has different logic for JUnit 4.10 and earlier versions, and for versions later than 4.10 due to some constraints in later versions: according to the official documentation
“Unfortunately JUnit 4.11 and above prohibits @Rule on static members so a slightly more verbose form is required.”
Since JUnit 5 Vintage requires JUnit 4.12 or later version to be on the classpath it is implicitly supported by the JUnit 4 specific actions.
Generally, these actions are available when only JUnit 4 is suitable for the current test class. More specific criteria are described in the table below.
All WireMock code templates are available under IDE Settings > Editor > File and Code Templates > Code.
WireMock JUnit 4 Rule
Available regardless of the library version.
@org.junit.Rule
public com.github.tomakehurst.wiremock.junit.WireMockRule ${NAME} = new WireMockRule()
Template variables:
${NAME}
: Name of the created field.
WireMock JUnit 4.10 Class Rule
Available when JUnit 4.10 or earlier (Not necessarily exclusively, other versions of 4.x might also be present.) is on the classpath.
@org.junit.ClassRule
@org.junit.Rule
public static com.github.tomakehurst.wiremock.junit.WireMockClassRule ${NAME} = new WireMockClassRule();
Template variables:
${NAME}
: Name of the created field.
WireMock JUnit 4.11 Class Rule
Available when JUnit 4.11 or later (Not necessarily exclusively, other versions of 4.x might also be present.) is on the classpath.
@org.junit.ClassRule
public static com.github.tomakehurst.wiremock.junit.WireMockClassRule ${CLASS_RULE_NAME} = new WireMockClassRule();
@org.junit.Rule
public WireMockClassRule ${RULE_NAME} = ${CLASS_RULE_NAME};
Template variables:
${CLASS_RULE_NAME}
: Name of the class rule field.${RULE_NAME}
: Name of the instance rule field.
NOTES:
- Upon insertion, all template variables suggest potentially suitable field names.