Skip to content

Commit 49a5454

Browse files
fix: PR review
Signed-off-by: SebastienDegodez <[email protected]>
1 parent c7b064c commit 49a5454

14 files changed

+92
-90
lines changed

.github/instructions/contract-testing-microcks-dotnet.instructions.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class OrderApiContractTest : BaseIntegrationTest
4141
{
4242
private readonly ITestOutputHelper TestOutputHelper;
4343

44-
public OrderApiContractTest(ITestOutputHelper testOutputHelper, MicrocksWebApplicationFactory<Program> factory)
44+
public OrderApiContractTest(ITestOutputHelper testOutputHelper, OrderServiceWebApplicationFactory<Program> factory)
4545
: base(factory)
4646
{
4747
TestOutputHelper = testOutputHelper;
@@ -73,14 +73,14 @@ public class OrderApiContractTest : BaseIntegrationTest
7373

7474
### Custom WebApplicationFactory: .NET 10+ and below
7575

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:
7777

7878
For .NET 10 and above, inherit from `WebApplicationFactory<TProgram>`:
7979
```csharp
80-
public class MicrocksWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>
80+
public class OrderServiceWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>
8181
where TProgram : class
8282
{
83-
public MicrocksWebApplicationFactory()
83+
public OrderServiceWebApplicationFactory()
8484
{
8585
// Even in .NET 10+, call UseKestrel() for consistency and explicitness
8686
UseKestrel();
@@ -90,7 +90,7 @@ public class MicrocksWebApplicationFactory<TProgram> : WebApplicationFactory<TPr
9090

9191
For .NET 9 and below, inherit from `KestrelWebApplicationFactory<TProgram>`:
9292
```csharp
93-
public class MicrocksWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
93+
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
9494
where TProgram : class
9595
{
9696
private const string MicrocksImage = "quay.io/microcks/microcks-uber:1.12.1";
@@ -158,14 +158,14 @@ public class MicrocksWebApplicationFactory<TProgram> : KestrelWebApplicationFact
158158
159159
### Base Integration Test with Proper Initialization
160160
```csharp
161-
public class BaseIntegrationTest : IClassFixture<MicrocksWebApplicationFactory<Program>>
161+
public class BaseIntegrationTest : IClassFixture<OrderServiceWebApplicationFactory<Program>>
162162
{
163163
public WebApplicationFactory<Program> Factory { get; private set; }
164164
public ushort Port { get; private set; }
165165
public MicrocksContainerEnsemble MicrocksContainerEnsemble { get; }
166166
public MicrocksContainer MicrocksContainer => MicrocksContainerEnsemble.MicrocksContainer;
167167

168-
protected BaseIntegrationTest(MicrocksWebApplicationFactory<Program> factory)
168+
protected BaseIntegrationTest(OrderServiceWebApplicationFactory<Program> factory)
169169
{
170170
Factory = factory;
171171
Port = factory.ActualPort;

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ You will work with a ASP.NET Core application and explore:
1616
- [Step 5: Write Tests for Async](step5-write-async-tests.md)
1717

1818
Complementary Resources:
19-
- [Integration Testing Patterns](step-integration-testing-patterns.md)
19+
- [Integration Testing Patterns](explain-integration-testing-patterns.md)
2020

2121
## License Summary
2222

explain-integration-testing-patterns.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This guide explains two different approaches for setting up integration tests wi
77
When writing integration tests that use Microcks and Kafka containers, you have two main architectural choices:
88

99
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
1111

1212
## Pattern 1: IClassFixture - Isolated Test Classes
1313

@@ -19,7 +19,7 @@ When writing integration tests that use Microcks and Kafka containers, you have
1919

2020
### Architecture
2121
```csharp
22-
public class MyTestClass : IClassFixture<MicrocksWebApplicationFactory<Program>>
22+
public class MyTestClass : IClassFixture<OrderServiceWebApplicationFactory<Program>>
2323
{
2424
// Each test class gets its own factory instance
2525
// Each factory starts its own containers
@@ -34,8 +34,15 @@ public class MyTestClass : IClassFixture<MicrocksWebApplicationFactory<Program>>
3434
### Implementation Example
3535

3636
#### 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.
3744
```csharp
38-
public class MicrocksWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
45+
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
3946
where TProgram : class
4047
{
4148
public ushort ActualPort { get; private set; }
@@ -102,13 +109,13 @@ public class MicrocksWebApplicationFactory<TProgram> : KestrelWebApplicationFact
102109

103110
#### Step 2: Test Class Implementation
104111
```csharp
105-
public class OrderControllerTests : IClassFixture<MicrocksWebApplicationFactory<Program>>
112+
public class OrderControllerTests : IClassFixture<OrderServiceWebApplicationFactory<Program>>
106113
{
107-
private readonly MicrocksWebApplicationFactory<Program> _factory;
114+
private readonly OrderServiceWebApplicationFactory<Program> _factory;
108115
private readonly ITestOutputHelper _testOutput;
109116

110117
public OrderControllerTests(
111-
MicrocksWebApplicationFactory<Program> factory,
118+
OrderServiceWebApplicationFactory<Program> factory,
112119
ITestOutputHelper testOutput)
113120
{
114121
_factory = factory;
@@ -153,15 +160,15 @@ public class OrderControllerTests : IClassFixture<MicrocksWebApplicationFactory<
153160
### Architecture
154161
```csharp
155162
[Collection(SharedTestCollection.Name)]
156-
public class MyTestClass : BaseIntegrationTest
163+
public class MyTestClass
157164
{
158165
// All test classes share the same factory instance
159166
// Single set of containers for all tests
160167
}
161168
```
162169

163170
### Key Benefits
164-
- **Single Container Instance**: One Microcks + one Kafka container for all tests
171+
- **Single set of containers Instances**: One Microcks + one Kafka container for all tests
165172
- **Performance Optimized**: ~70% faster test execution
166173
- **Resource Efficient**: Lower memory and CPU usage
167174
- **Single Port Allocation**: One Kestrel port for the entire test suite
@@ -171,15 +178,15 @@ public class MyTestClass : BaseIntegrationTest
171178
#### Step 1: Shared Collection Definition
172179
```csharp
173180
[CollectionDefinition(Name)]
174-
public class SharedTestCollection : ICollectionFixture<MicrocksWebApplicationFactory<Program>>
181+
public class SharedTestCollection : ICollectionFixture<OrderServiceWebApplicationFactory<Program>>
175182
{
176183
public const string Name = "SharedTestCollection";
177184
}
178185
```
179186

180187
#### Step 2: Enhanced WebApplicationFactory
181188
```csharp
182-
public class MicrocksWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
189+
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
183190
where TProgram : class
184191
{
185192
private static readonly SemaphoreSlim InitializationSemaphore = new(1, 1);
@@ -250,7 +257,7 @@ public abstract class BaseIntegrationTest
250257
public KafkaContainer KafkaContainer { get; }
251258
public HttpClient HttpClient { get; private set; }
252259

253-
protected BaseIntegrationTest(MicrocksWebApplicationFactory<Program> factory)
260+
protected BaseIntegrationTest(OrderServiceWebApplicationFactory<Program> factory)
254261
{
255262
Factory = factory;
256263
HttpClient = factory.CreateClient();
@@ -274,7 +281,7 @@ public class OrderControllerTests : BaseIntegrationTest
274281

275282
public OrderControllerTests(
276283
ITestOutputHelper testOutput,
277-
MicrocksWebApplicationFactory<Program> factory)
284+
OrderServiceWebApplicationFactory<Program> factory)
278285
: base(factory)
279286
{
280287
_testOutput = testOutput;

step4-write-rest-tests.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Let's fix that!!
99

1010
For all the integration tests in our application, we need to start Kafka and use mocks provided by Microcks containers.
1111

12-
### Review MicrocksWebApplicationFactory<Program> class under tests/Order.Service.Tests
12+
### Review OrderServiceWebApplicationFactory<Program> class under tests/Order.Service.Tests
1313

1414
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.
1515

@@ -22,7 +22,7 @@ Here's how it works:
2222

2323
Example:
2424
```csharp
25-
public class MicrocksWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
25+
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
2626
where TProgram : class
2727
{
2828
// ...existing code...
@@ -74,7 +74,7 @@ This setup means you do not need to manually start Kafka, Microcks, or any other
7474

7575
Let's understand what this configuration class does:
7676

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.
7878
* `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.
7979
In detail:
8080
* 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
128128

129129
public PastryAPIClientTests(
130130
ITestOutputHelper testOutputHelper,
131-
MicrocksWebApplicationFactory<Program> factory)
131+
OrderServiceWebApplicationFactory<Program> factory)
132132
: base(factory)
133133
{
134134
TestOutputHelper = testOutputHelper;

step5-write-async-tests.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class OrderKafkaContractTests : BaseIntegrationTest
2525

2626
public OrderKafkaContractTests(
2727
ITestOutputHelper testOutputHelper,
28-
MicrocksWebApplicationFactory<Program> factory)
28+
OrderServiceWebApplicationFactory<Program> factory)
2929
: base(factory)
3030
{
3131
TestOutputHelper = testOutputHelper;
@@ -181,7 +181,7 @@ public class OrderEventListenerTests : BaseIntegrationTest
181181

182182
public OrderEventListenerTests(
183183
ITestOutputHelper testOutputHelper,
184-
MicrocksWebApplicationFactory<Program> factory)
184+
OrderServiceWebApplicationFactory<Program> factory)
185185
: base(factory)
186186
{
187187
TestOutputHelper = testOutputHelper;

tests/Order.Service.Tests/Api/OrderControllerContractTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class OrderControllerContractTests : BaseIntegrationTest
3434

3535
public OrderControllerContractTests(
3636
ITestOutputHelper testOutputHelper,
37-
MicrocksWebApplicationFactory<Program> factory)
37+
OrderServiceWebApplicationFactory<Program> factory)
3838
: base(factory)
3939
{
4040
TestOutputHelper = testOutputHelper;

tests/Order.Service.Tests/Api/OrderControllerPostmanContractTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class OrderControllerPostmanContractTests : BaseIntegrationTest
3030

3131
public OrderControllerPostmanContractTests(
3232
ITestOutputHelper testOutputHelper,
33-
MicrocksWebApplicationFactory<Program> factory)
33+
OrderServiceWebApplicationFactory<Program> factory)
3434
: base(factory)
3535
{
3636
TestOutputHelper = testOutputHelper;

tests/Order.Service.Tests/BaseIntegrationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
namespace Order.Service.Tests;
2727

2828
/// <summary>
29-
/// Base class for integration tests using a shared MicrocksWebApplicationFactory instance.
29+
/// Base class for integration tests using a shared OrderServiceWebApplicationFactory instance.
3030
/// All tests inheriting from this class will share the same factory instance across the entire test assembly.
3131
/// </summary>
3232
[Collection(SharedTestCollection.Name)]
@@ -41,7 +41,7 @@ public abstract class BaseIntegrationTest
4141
public KafkaContainer KafkaContainer { get; }
4242
public HttpClient? HttpClient { get; private set; }
4343

44-
protected BaseIntegrationTest(MicrocksWebApplicationFactory<Program> factory)
44+
protected BaseIntegrationTest(OrderServiceWebApplicationFactory<Program> factory)
4545
{
4646
Factory = factory;
4747

tests/Order.Service.Tests/Client/PastryAPIClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class PastryAPIClientTests : BaseIntegrationTest
3636

3737
public PastryAPIClientTests(
3838
ITestOutputHelper testOutputHelper,
39-
MicrocksWebApplicationFactory<Program> factory)
39+
OrderServiceWebApplicationFactory<Program> factory)
4040
: base(factory)
4141
{
4242
TestOutputHelper = testOutputHelper;

tests/Order.Service.Tests/Order.Service.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Awaitility.NET" Version="1.0.2" />
910
<PackageReference Include="coverlet.collector" Version="6.0.4">
1011
<PrivateAssets>all</PrivateAssets>
1112
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)