-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@InjectWireMock not working in conjunction with Cucumber #73
Comments
I'm attaching the project files |
I think the problem here is that the Junit5-extension is not triggered. None of the functionality it provides is available. Not sure if that can, or should, be fixed here. But perhaps you only need the Spring features. Something like: public class ApplicationSteps {
@Value("${wiremock.server.port}")
private int wiremockPort;
@When("foo")
public void foo() {
WireMock.configureFor(wiremockPort);
WireMock.reset(); // Not sure where, or if, you want/need this.
// Configure with code maby?
WireMock.stubFor(get("/ping").willReturn(aResponse().withStatus(200)));
}
} And if you don't want to configure with code, it will still look for configuration as stated in the log:
|
Thanks for the quick feedback. I tried your suggestion @CucumberContextConfiguration
@EnableWireMock
@SpringBootTest
public class ApplicationSteps {
@Value("${wiremock.server.port}")
private int wiremockPort;
@When("foo")
public void foo() {
WireMock.reset();
WireMock.configureFor(wiremockPort);
// assertThat(wireMockServer).isNotNull();
}
} but I'm getting the following exception:
|
Put reset after configureFor. I updated my comment with that. |
This does seem to work. Thanks a lot! The issue can be closed. You might want to consider adding this to the documentation. |
I have a good workaround now, but I was wondering why don't you add the WireMockServer bean to the Spring context (just like you add those two properties), so that it can simply be injected via |
In an earlier revision of the readme: It says "does not pollute spring application context with extra beans". But I dont know exactly what the problem was, it seems intended. |
@maciejwalkowiak what was your original motivation for keeping WireMock out of the Spring context? Have you found it added significant overhead to do it that way? |
The original motivation was to avoid amending Spring context and risk slowing down tests because of that (basically avoid what Perhaps there are cases like usage with Cucumber where it would make more sense to add it as a bean. |
I'd like an example of such case. When we write integration tests, we create separate beans in test context which encapsulate some logic. One example may be an HTTP fixture, which manipulates wiremock. So the simplified setup looks like this: class SomeTest extends AbstractIntegrationTest {
@Test
void test() {
httpFixture.setUp();
// proceed with test
}
}
@SpringBootTest
class AbstractIntegrationTest {
@Autowired protected HttpServerMockFixture httpFixture;
}
@Component
class HttpServerMockFixture {
private final WireMockServer wireMockServer;
HttpServerMockFixture(@InjectWireMock WireMockServer wireMockServer) {
this.wireMockServer = wireMockServer;
}
void setUp() {
// wireMockServer manipulation
}
}
This example fails as WireMockServer is not a bean and `@InjectWireMock` works only in test classes. If `WireMockServer` is a singleton, I don't actually see downsides, as spring context isn't invalidated for every test. |
Sure, perhaps the benefits overweight the downsides. There are some edge cases to handle - when multiple WireMock servers are registered as beans, you need a qualifier to inject them etc. @tomakehurst if it is beneficial for the project and community to register servers as beans I don't see a reason to not do it. The only thing that I believe should stay is registering multiple servers in single project as this is something that distinguishes this project from Spring Cloud WireMock. |
It sounds like we could give the Cucumber folks a helping hand if we did this, and maybe if it was only activated if you explicitly opt into it then we avoid the overhead in cases where we don't need a bean? Maybe we could just say that you have to give it a qualifier if you want it in the Spring context, and if you omit that parameter it won't be added? |
Proposal
I'm testing my spring boot application with Cucumber. Unfortunately, @InjectWireMock doesn't seem to be working in that case.
Reproduction steps
I have a feature file in src/test/resources/features
An application class in src/man/java:
A test class in src/test/java
And a steps class also in src/test/java:
The test fails since wireMockServer is null. I'm using Java 21 and the latest versions of Spring Boot (3.4.0), Cucumber (7.20.1) and WireMock Spring Boot (3.4.0).
References
No response
The text was updated successfully, but these errors were encountered: