500 Internal Server Error when Testing CreateCategory Endpoint in ASP.NET Core

I’m working on an ASP.NET Core project where I’m trying to create an integration test for an endpoint that creates a new category. Despite following the implementation steps, my test consistently fails with a 500 Internal Server Error. I have added detailed logging to diagnose the issue, but I still can’t identify the root cause

Why is my CreateCategory endpoint returning a 500 Internal Server Error instead of 204 No Content for valid requests?
Is there any specific configuration I might be missing in my test setup or the application itself?
Could there be any dependency injection or configuration issues that are not immediately apparent from the code provided?

Any help or pointers would be greatly appreciated. Thank you!
This is btw my first post so if did break any rule pls tell me!

[HttpPost]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public async Task<IActionResult> CreateCategory([FromBody] CreateCategoryDto categoryCreate)
{
    if (categoryCreate == null || string.IsNullOrEmpty(categoryCreate.Name))
    {
        return BadRequest("Invalid category data.");
    }

    try
    {
        _logger.LogInformation("Received request to create category: {@CategoryCreate}", categoryCreate);
        
        var command = new CreateCategoryCommand(categoryCreate.Id, categoryCreate.Name, categoryCreate.PokemonCategories);
        await _mediator.Send(command);
        return NoContent();
    }
    catch (EntityAlreadyExistsException<CategoryDto> ex)
    {
        _logger.LogError(ex, "Entity already exists.");
        return BadRequest(ex.Message);
    }
    catch (EntityFailedToCreateException<CategoryDto> ex)
    {
        _logger.LogError(ex, "Failed to create entity.");
        return StatusCode(500, ex.Message);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Error creating category.");
        return StatusCode(500, "Internal server error.");
    }
}

public class CreateCategoryCommandHandler : IRequestHandler<CreateCategoryCommand, Unit>
{
    private readonly ILogger<CreateCategoryCommandHandler> _logger;
    private readonly ICategoryRepository _categoryRepository;
    private readonly IMapper<Models.Category, CategoryDto> _mapper;

    public CreateCategoryCommandHandler(ILogger<CreateCategoryCommandHandler> logger, ICategoryRepository categoryRepository, IMapper<Models.Category, CategoryDto> mapper)
    {
        _logger = logger;
        _categoryRepository = categoryRepository;
        _mapper = mapper;
    }

    public async Task<Unit> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
    {
        _logger.LogInformation("Attempting to create a new category");

        try
        {
            var categories = await _categoryRepository.GetCategoriesAsync();
            var categoryExist = categories.FirstOrDefault(c => c.Name.Trim().ToUpper() == request.Name.Trim().ToUpper());

            if (categoryExist != null)
            {
                _logger.LogWarning("Category with name {Name} already exists", request.Name);
                throw new EntityAlreadyExistsException<CategoryDto>("Entity exists already");
            }

            var categoryToCreate = new Models.Category()
            {
                Id = request.Id,
                Name = request.Name
            };

            var created = await _categoryRepository.CreateCategory(categoryToCreate);

            if (!created)
            {
                _logger.LogError("Failed to create category with name {Name}", request.Name);
                throw new EntityFailedToCreateException<CategoryDto>($"Failed to create entity");
            }

            _logger.LogInformation("Category with name {Name} created successfully", request.Name);

            return Unit.Value;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error in CreateCategoryCommandHandler");
            throw;
        }
    }
}

using System.Net;
using System.Net.Http.Json;
using FluentAssertions;
using IntegrationsTests.Pokemon;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PokemonAPI.Dto;
using Xunit.Abstractions;

namespace IntegrationsTests.CategoryTests.CreateTests
{
    public class CreateCategoryEndpointTests : IClassFixture<PokemonApiWebApplicationFactory>
    {
        private readonly ITestOutputHelper _testOutputHelper;
        private readonly HttpClient _client;

        public CreateCategoryEndpointTests(PokemonApiWebApplicationFactory factory, ITestOutputHelper testOutputHelper)
        {
            _testOutputHelper = testOutputHelper;
            _client = factory.CreateClient();
        }

        [Fact]
        public async Task CreateCategory_EndpointReturnsNoContentForValidRequest()
        {
            // Arrange
            var createDto = new CreateCategoryDto
            {
                Id = 1,
                Name = "New Category",
                PokemonCategories = new List<PokemonCategoryDto>
                {
                    new PokemonCategoryDto { PokemonId = 1, CategoryId = 1 }
                }
            };

            var requestContent = JsonConvert.SerializeObject(createDto);
            _testOutputHelper.WriteLine($"Sending request: {requestContent}");

            // Act
            var response = await _client.PostAsJsonAsync("/api/category", createDto);

            var responseContent = await response.Content.ReadAsStringAsync();
            _testOutputHelper.WriteLine($"Response status code: {response.StatusCode}");
            _testOutputHelper.WriteLine($"Response content: {responseContent}");

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.NoContent);
        }

        [Fact]
        public async Task CreateCategory_EndpointReturnsBadRequestForInvalidRequest()
        {
            // Arrange
            var createDto = new CreateCategoryDto
            {
                Name = null,
                PokemonCategories = new List<PokemonCategoryDto>
                {
                    new PokemonCategoryDto { PokemonId = 1, CategoryId = 1 }
                }
            };

            var requestContent = JsonConvert.SerializeObject(createDto);
            _testOutputHelper.WriteLine($"Sending invalid request: {requestContent}");

            // Act
            var response = await _client.PostAsJsonAsync("/api/category", createDto);

            var responseContent = await response.Content.ReadAsStringAsync();
            _testOutputHelper.WriteLine($"Response status code: {response.StatusCode}");
            _testOutputHelper.WriteLine($"Response content: {responseContent}");

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
        }
    }
}

When running the integration test for a valid request, I see the following output:
Sending request: {“Id”:1,”Name”:”New Category”,”PokemonCategories”:[{“PokemonId”:1,”CategoryId”:1}]}
Response status code: InternalServerError
Response content: Internal server error.
Error Details:

Xunit.Sdk.XunitException
Expected response.StatusCode to be HttpStatusCode.NoContent {value: 204}, but found HttpStatusCode.InternalServerError {value: 500}.
at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message)
at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message)
at FluentAssertions.Execution.AssertionScope.FailWith(Func1 failReasonFunc) at FluentAssertions.Execution.AssertionScope.FailWith(Func1 failReasonFunc)
at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args)
at FluentAssertions.Primitives.EnumAssertions2.Be(TEnum expected, String because, Object[] becauseArgs) at IntegrationsTests.CategoryTests.CreateTests.CreateCategoryEndpointTests.CreateCategory_EndpointReturnsNoContentForValidRequest() at Xunit.Sdk.TestInvoker1.<>c__DisplayClass48_0.<b__1>d.MoveNext() in //src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 276
— End of stack trace from previous location —
at Xunit.Sdk.ExecutionTimer.AggregateAsync(Func1 asyncAction) in /_/src/xunit.execution/Sdk/Frameworks/ExecutionTimer.cs:line 48 at Xunit.Sdk.ExceptionAggregator.RunAsync(Func1 code) in /
/src/xunit.core/Sdk/ExceptionAggregator.cs:line 90
Additional Information
The CreateCategoryDto object is correctly serialized and sent in the request body.
The handler checks for existing categories and creates a new one if it doesn’t exist.
The repository method CreateCategory returns a boolean indicating success or failure.
Exception handling is in place to catch and log errors.

New contributor

Suleyman Ceylan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật