You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Helidon currently lacks an equivalent to Spring Boot's @MockBean annotation, which allows for the injection of mock beans into the application context, ensuring that mock dependencies are used when running the application. This feature is essential for improving integration tests in Helidon.
Sample Usecase: Pact (Contract Testing)
One critical use case for this enhancement is contract testing, such as Pact testing, where microservices need to be tested in isolation to ensure their interactions conform to predefined contracts.
Suppose there are two microservices, X and Y, where X calls Y. Consumer tests are created by microservice X, specifying the endpoint, request, and expected response. For such tests to be effective, we need the ability to inject mock dependencies into the running server.
This consumer test is run at the microservice Y, to valid if the request returns the expected response (In the above example POST to /v1/product/check with the provided request should return a status 200)
Spring Boot Example
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Provider("Y")
public class PricingServiceProviderPactTest {
@MockBean
private ProductClient productClient; // Replaces the bean with a mock in the application context
@LocalServerPort
private int port;
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", port, "/"));
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) {
context.verifyInteraction();
}
@State("Product X010000021 exists")
public void setupProductX010000021() {
when(productClient.fetch((Set<String>) argThat(contains("X010000021")), any())).thenReturn(product);
}
}
In Spring Boot, the server starts, and the ProductClient is injected with a mock. When Pact runs the consumer tests, it calls the REST endpoint of the server, and since we can mock the injections, we can return the required data without actually performing the operation.
Helidon Example
In contrast, in Helidon, for a running server, you cannot inject a MockBean. Therefore, the only way to test the Pact is to create a new server with a mocked endpoint class. However, this approach does not constitute real integration testing, as the actual REST endpoint is not called. Consequently, if any changes occur in the server's path, the consumer test may not break, as the endpoint is manually specified in the test router.
Problem
Helidon currently lacks an equivalent to Spring Boot's
@MockBean
annotation, which allows for the injection of mock beans into the application context, ensuring that mock dependencies are used when running the application. This feature is essential for improving integration tests in Helidon.Sample Usecase: Pact (Contract Testing)
One critical use case for this enhancement is contract testing, such as Pact testing, where microservices need to be tested in isolation to ensure their interactions conform to predefined contracts.
Suppose there are two microservices, X and Y, where X calls Y. Consumer tests are created by microservice X, specifying the endpoint, request, and expected response. For such tests to be effective, we need the ability to inject mock dependencies into the running server.
Sample consumer test
This consumer test is run at the microservice Y, to valid if the request returns the expected response (In the above example POST to /v1/product/check with the provided request should return a status 200)
Spring Boot Example
In Spring Boot, the server starts, and the ProductClient is injected with a mock. When Pact runs the consumer tests, it calls the REST endpoint of the server, and since we can mock the injections, we can return the required data without actually performing the operation.
Helidon Example
In contrast, in Helidon, for a running server, you cannot inject a MockBean. Therefore, the only way to test the Pact is to create a new server with a mocked endpoint class. However, this approach does not constitute real integration testing, as the actual REST endpoint is not called. Consequently, if any changes occur in the server's path, the consumer test may not break, as the endpoint is manually specified in the test router.
SampleProviderTest
SampleEndpointMocked
The text was updated successfully, but these errors were encountered: