0

I have a dotnet project that I've been working on adding unit tests to - I took over the project somewhat recently, and the original team that managed it had some pretty lazy requirements about some things. In any case, in JetBrains Rider (and Visual Studio) when I run the unit tests, everything works fine. If I go to the directory in a terminal session and run 'dotnet test' everything works fine. In my dockerfile, adding the command after the build command correctly calls to run the tests, but it fails.

So: the project is structured fairly odd. It's split into 4 sub-projects, with the test framework set into its own sub-project. The docker file:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0.402 AS build
WORKDIR /src
COPY ["<company>.Web.csproj", "<company>.Web/"]
COPY ["<company>.Business.csproj", "<company>.Business/"]
COPY ["<company>.Storage.csproj", "<company>.Storage/"]
COPY ["<company>.Middleware.csproj", "<company>.Middleware/"]
COPY ["<company>.WebTest.csproj", "<company>.WebTest/"]
RUN dotnet restore "<company>.Web.csproj"
COPY . .
WORKDIR "/src/<company>.Web"
RUN dotnet build "<company>.Web.csproj" -c Release -o /app/build

FROM build AS test
WORKDIR "/src/<company>.WebTest"
RUN dotnet test

FROM test AS publish
WORKDIR "/src/<company>.Web"
RUN dotnet publish "<company>.Web.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "<company>.Web.dll"]

Sample output from running dotnet test:

[08:53:09] <machine> [<dir>.WebTest]: dotnet test
  Determining projects to restore...
  All projects are up-to-date for restore.
  <company>.Middleware -> <dir>.Middleware\bin\Debug\net6.0\<company>.Middleware.dll
  <company>.Storage -> <dir>.Storage\bin\Debug\net6.0\<company>.Storage.dll
  <company>.Business -> <dir>.Business\bin\Debug\net6.0\<company>.Business.dll
  <company>.WebTest -> <dir>.WebTest\bin\Debug\net6.0\<company>.WebTest.dll
Test run for <dir>.WebTest\bin\Debug\net6.0\<company>.WebTest.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.5.0 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:    58, Skipped:     0, Total:    58, Duration: 2 s - <company>.WebTest.dll (net6.0)

Output from the docker build is the same, up until this happens:

#21 6.356 Starting test execution, please wait...
#21 6.373 A total of 1 test files matched the specified pattern.
#21 7.413 The active test run was aborted. Reason: Test host process crashed : Unhandled exception. Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.

The NullReference is to an object that the test execution is Mocking - so it seems like it's not initializing the tests for some reason, as the Mocks are constructed in [OneTimeSetUp] and [SetUp] annotated methods.

Any ideas? Just FYI, by the way, I'm far more experienced with Java - this is my first foray into c#/dotnet, so it's entirely possible I'm just missing something.

rdejulis
  • 11
  • 1
  • Without actual code and or [mre] it is hard to say. The only option I see is to close it as duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Guru Stron May 15 '23 at 15:27
  • I wouldn't call it a duplicate of that, because the problem isn't necessarily that I don't know how to deal with a NullReferenceException - it's only getting that error when running the unit tests as part of the docker build. Running the unit tests in any other fashion works fine. – rdejulis May 15 '23 at 16:01
  • _"it's only getting that error when running the unit tests as part of the docker build."_ - it is still a NRE, so you need to deal with NRE. "Works on my machine" does not fix NREs in production environment. And it does not fix it in test env inside docker also. Can you please provide a [mre]? – Guru Stron May 15 '23 at 16:44
  • I think you're misunderstanding. I exposed a potential NullReference, which is easily fixed - but then the test fails, because the NullReference wasn't ever the actual issue. The issue is that the docker build does not seem to be initializing the mocked object (which is initialized via dependency injection outside of the unit tests). I'm working on getting an MRE ready, but it will take some time. – rdejulis May 15 '23 at 18:09
  • Please provide a [mre]. Since this happens inside the docker it should be very easy to create one. – Guru Stron May 15 '23 at 18:12
  • In setting up the MRE, I discovered the actual issue - running it in the docker container handles the mock Setup() calls differently apparently, so previous Setups were sitting around causing the code to branch where it shouldn't have... resulting in a call that wasn't Setup() throwing the NPE. I hope that makes sense. In any case, thanks for attempting to help. – rdejulis May 15 '23 at 20:19

0 Answers0