Skip to content

Commit 3ffd6bf

Browse files
build: upgrade to .NET 10 and remove KestrelWebApplicationFactory (microcks#74)
* Initial plan * build: upgrade to .NET 10 and update NuGet packages - Update global.json SDK version from 9.0.111 to 10.0.201 - Update Directory.Packages.props: Microsoft.AspNetCore.OpenApi and Microsoft.AspNetCore.Mvc.Testing from 9.0.14 to 10.0.5 - Update Order.Service.csproj and Order.Service.Tests.csproj target frameworks from net9.0 to net10.0 Signed-off-by: microcks-bot <copilot@github.com> Agent-Logs-Url: https://github.com/microcks/microcks-testcontainers-dotnet-demo/sessions/1ba00e8d-1c66-413b-9715-faddf600c2f1 Co-authored-by: SebastienDegodez <2349146+SebastienDegodez@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SebastienDegodez <2349146+SebastienDegodez@users.noreply.github.com>
1 parent f586ec4 commit 3ffd6bf

8 files changed

Lines changed: 11 additions & 146 deletions

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
<ItemGroup>
88
<!-- AspNetCore & Web -->
9-
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.14" />
10-
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.14" />
9+
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.5" />
10+
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.5" />
1111

1212
<!-- Testing Framework -->
1313
<PackageVersion Include="xunit.v3" Version="3.2.2" />

explain-integration-testing-patterns.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class MyTestClass : IClassFixture<OrderServiceWebApplicationFactory<Progr
4242
>
4343
> - 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.
4444
```csharp
45-
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
45+
public class OrderServiceWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>, IAsyncLifetime
4646
where TProgram : class
4747
{
4848
public ushort ActualPort { get; private set; }
@@ -186,7 +186,7 @@ public class SharedTestCollection : ICollectionFixture<OrderServiceWebApplicatio
186186

187187
#### Step 2: Enhanced WebApplicationFactory
188188
```csharp
189-
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
189+
public class OrderServiceWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>, IAsyncLifetime
190190
where TProgram : class
191191
{
192192
private static readonly SemaphoreSlim InitializationSemaphore = new(1, 1);

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "9.0.111",
3+
"version": "10.0.201",
44
"rollForward": "latestMinor"
55
}
66
}

src/Order.Service/Order.Service.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
</PropertyGroup>
77

step4-write-rest-tests.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Here's how it works:
2222

2323
Example:
2424
```csharp
25-
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
25+
public class OrderServiceWebApplicationFactory<TProgram> : WebApplicationFactory<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-
* `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.
77+
* `OrderServiceWebApplicationFactory` extends `WebApplicationFactory<TProgram>` to provide a custom test environment for our application. In .NET 10, `WebApplicationFactory` natively supports Kestrel via the `UseKestrel()` method, so no custom wrapper class is needed.
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.

tests/Order.Service.Tests/KestrelWebApplicationFactory.cs

Lines changed: 0 additions & 136 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net9.0</TargetFramework>
3+
<TargetFramework>net10.0</TargetFramework>
44
<Nullable>enable</Nullable>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>

tests/Order.Service.Tests/OrderServiceWebApplicationFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using Microcks.Testcontainers.Connection;
3030

3131
using Microsoft.AspNetCore.Hosting;
32+
using Microsoft.AspNetCore.Mvc.Testing;
3233
using Microsoft.Extensions.Hosting;
3334

3435
using Testcontainers.Kafka;
@@ -42,7 +43,7 @@ namespace Order.Service.Tests;
4243
/// This factory is designed to be used as a singleton across all test classes to optimize container startup time.
4344
/// Containers are started once and reused by all tests in the test assembly.
4445
/// </summary>
45-
public class OrderServiceWebApplicationFactory<TProgram> : KestrelWebApplicationFactory<TProgram>, IAsyncLifetime
46+
public class OrderServiceWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram>, IAsyncLifetime
4647
where TProgram : class
4748
{
4849
private const string MicrocksImage = "quay.io/microcks/microcks-uber:1.13.0-native";

0 commit comments

Comments
 (0)