Closed davidkeaveny closed 11 months ago
I am unable to reproduce the issue, and I do not think GetConnectionString()
can return null
at all:
Considering the stack trace, it looks like the ArgumentNullException
is not thrown from the tests (ConfigureDirectoryDbContext(IServiceCollection)
). It is thrown from your main application.
System.ArgumentNullException : Value cannot be null. (Parameter 'connectionString')
Stack Trace:
at System.ArgumentNullException.Throw(String paramName)
at System.ArgumentNullException.ThrowIfNull(Object argument, String paramName)
at MyProject.Shared.Database.Directory.ServiceCollectionExtensions.AddDirectoryDbContext(IServiceCollection services, String connectionString) in /home/runner/work/api/api/shared/src/MyProject.Shared.Database.Directory/ServiceCollectionExtensions.cs:line 18
at Program.<Main>$(String[] args) in /home/runner/work/api/api/api/src/MyProject.Api.Application/Program.cs:line 127
I do not think this is a TC for .NET issue. Can you share a reproducer?
You're right - the TestApiApplication
was verifying that a connection string had been set in configuration, and throwing an ArgumentNullException
if no connection string was present. Seeing as this is the 21st century and nice developers don't store their connection strings in plaintext to expose in their GitHub repositories, the connection string was in user secrets on my dev PC, which are obviously not going to be present when running in GitHub Actions, hence the error before the test framework could do its bit and swap out the default implementation with the TestContainer-backed implementation.
So I modified the call to dotnet test
in the GitHub Action to set a default connection string in an environment variable, now the tests Just Work TM.
Sorry to waste your time, and thank you for producing such a useful tool!
Testcontainers version
3.5.0
Using the latest Testcontainers version?
Yes
Host OS
Linux
Host arch
x86
.NET version
6.0.22
Docker version
Docker info
What happened?
I'm using TestContainer to test a .NET Core 6 API, that uses two database containers (Postgres and SQL Server). The API is a multi-tenanted application (database per tenant); the Postgres database stores a directory of the tenant databases (which are hosted in SQL Server). When I run my tests through TestContainer on my dev PC, the tests all pass. However, when I then run the same tests as part of my CI workflow in GitHub Actions, then the tests fail on set up with an
ArgumentNullException
when initialising theDbContext
with the connection string retrieved from the test container.I've attached a sample log from GitHub Actions, as well as the code for my TestContainer setup. The exception is being thrown during that setup, because
directoryConnectionString
is being set as null when calling_postgresContainer.GetConnectionString()
. Would the reason for that be that Postgres hasn't finished initialising, or somesuch (although the logs suggest readiness checks have been done)?Relevant log output
Additional information
TestContainers are initialised as follows: