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
@@ -73,14 +73,14 @@ public class OrderApiContractTest : BaseIntegrationTest
73
73
74
74
### Custom WebApplicationFactory: .NET 10+ and below
75
75
76
-
For both .NET 10+ and below, you must call `UseKestrel()` in your `MicrocksWebApplicationFactory` to ensure real HTTP server testing for contract tests. The only difference is the base class:
76
+
For both .NET 10+ and below, you must call `UseKestrel()` in your `OrderServiceWebApplicationFactory` to ensure real HTTP server testing for contract tests. The only difference is the base class:
77
77
78
78
For .NET 10 and above, inherit from `WebApplicationFactory<TProgram>`:
Copy file name to clipboardExpand all lines: explain-integration-testing-patterns.md
+19-12Lines changed: 19 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ This guide explains two different approaches for setting up integration tests wi
7
7
When writing integration tests that use Microcks and Kafka containers, you have two main architectural choices:
8
8
9
9
1.**IClassFixture Pattern**: Multiple container instances, isolated per test class
10
-
2.**ICollectionFixture Pattern**: Single shared container instance, optimized for performance
10
+
2.**ICollectionFixture Pattern**: A set of shared container instances (e.g., one Microcks container and one Kafka container) used by all test classes for optimal performance and resource efficiency
11
11
12
12
## Pattern 1: IClassFixture - Isolated Test Classes
13
13
@@ -19,7 +19,7 @@ When writing integration tests that use Microcks and Kafka containers, you have
@@ -34,8 +34,15 @@ public class MyTestClass : IClassFixture<MicrocksWebApplicationFactory<Program>>
34
34
### Implementation Example
35
35
36
36
#### Step 1: WebApplicationFactory with Dynamic Ports
37
+
38
+
> **Note:**
39
+
> The implementation of `OrderServiceWebApplicationFactory` (or `OrderServiceWebApplicationFactory`) must be adapted depending on the fixture pattern:
40
+
>
41
+
> - With **IClassFixture**, each test class gets its own factory and containers. You must allocate **dynamic ports** for Kestrel, Kafka, and all services, because static port mapping (like `kafka:9092:9092`) is not possible—otherwise, you will have port conflicts if tests run in parallel. All port assignments must be programmatic and injected into your test server and mocks.
42
+
>
43
+
> - With **ICollectionFixture** (shared collection), a single factory and set of containers are shared for all tests. You allocate ports only once, which simplifies configuration and avoids conflicts. This is why the shared collection pattern is recommended for most test suites.
Copy file name to clipboardExpand all lines: step4-write-rest-tests.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ Let's fix that!!
9
9
10
10
For all the integration tests in our application, we need to start Kafka and use mocks provided by Microcks containers.
11
11
12
-
### Review MicrocksWebApplicationFactory<Program> class under tests/Order.Service.Tests
12
+
### Review OrderServiceWebApplicationFactory<Program> class under tests/Order.Service.Tests
13
13
14
14
In .NET, we use a custom `WebApplicationFactory<Program>` to provision all required containers and inject configuration for local development and testing. This leverages the .NET Testcontainers library and ASP.NET Core's test infrastructure.
@@ -74,7 +74,7 @@ This setup means you do not need to manually start Kafka, Microcks, or any other
74
74
75
75
Let's understand what this configuration class does:
76
76
77
-
*`MicrocksWebApplicationFactory` extends `KestrelWebApplicationFactory<TProgram>` to provide a custom test environment for our application. `KestrelWebApplicationFactory` is a solution to expose the application on the real host port. In the .NET 10 (under development), this class can be replaced by the `WebApplicationFactory<TProgram>` class.
77
+
*`OrderServiceWebApplicationFactory` extends `KestrelWebApplicationFactory<TProgram>` to provide a custom test environment for our application. `KestrelWebApplicationFactory` is a solution to expose the application on the real host port. In the .NET 10 (under development), this class can be replaced by the `WebApplicationFactory<TProgram>` class.
78
78
*`InitializeAsync` method is called to set up the test environment before running tests. It allocates a free port, starts the Kafka container, and initializes the Microcks container ensemble with the required artifacts.
79
79
In detail:
80
80
* We allocate a free port and expose it for container communication, in .NET it's possible to assign a dynamic port to the kestrel server but when we use Microcks Contract Testing, it's necessary to call the `ExposeHostPortsAsync` method to expose the port for host communication.
@@ -128,7 +128,7 @@ public class PastryAPIClientTests : BaseIntegrationTest
0 commit comments