Skip to content

Implementação de uma Minimal API com Entity framework e Testes Automatizados em .NET 8

Notifications You must be signed in to change notification settings

andrenunes57/minimal-api

Repository files navigation

MinimalApi

🇧🇷 Instruções em Português

Este projeto contém uma API mínima em ASP.NET Core (.NET 8), além de projetos de teste unitário e de integração.

Como executar a MinimalApi

  1. Instale o .NET 8 SDK.
  2. Certifique-se de ter o MySQL instalado e rodando em sua máquina, pois ele é o banco configurado na aplicação.
  3. Configure a string de conexão na chave ConnectionStrings:DefaultConnection do appsettings.json (ou via secrets do usuário). Exemplo genérico (substitua <usuario> e <senha> pelos seus dados do MySQL):
    "ConnectionStrings": {
      "DefaultConnection": "Server=localhost;Database=MinimalApiDb;User Id=<usuario>;Password=<senha>;"
    }
  4. Configure a chave JWT na chave Jwt:Key do appsettings.json (ou via secrets do usuário). Para geração do token JWT é utilizado o algoritmo HS256, sendo necessário informar uma string com no mínimo 32 caracteres. Já existe uma chave de exemplo no arquivo para desenvolvimento:
    "Jwt": {
      "Key": "MySecretKeyForJWT123456789012345"
    }
  5. Certifique-se de estar na pasta raiz do projeto (onde está o arquivo .sln) para que os comandos funcionem corretamente.
  6. Execute o comando:
    dotnet run --project MinimalApi/MinimalApi.csproj
  7. A aplicação irá rodar nas portas especificadas na chave https:applicationUrl do arquivo MinimalApi/Properties/launchSettings.json:

Testes Unitários

  • Os testes unitários estão em MinimalApi.UnitTests.
  • Para rodar, esteja na pasta raiz do projeto:
    dotnet test MinimalApi.UnitTests/MinimalApi.UnitTests.csproj

Testes de Integração

  • Os testes de integração estão em MinimalApi.IntegrationTests.
  • Para rodar, esteja na pasta raiz do projeto:
    dotnet test MinimalApi.IntegrationTests/MinimalApi.IntegrationTests.csproj
  • Importante: Os testes de integração utilizam o Testcontainers para criar um banco MySQL temporário via Docker. Certifique-se de ter o Docker instalado e em execução. Isso elimina a necessidade de um banco de dados de teste manual, pois o container é criado e descartado automaticamente a cada rodada de teste, criando uma cópia do banco da aplicação para os testes.

🇺🇸 Instructions in English

This project contains a minimal ASP.NET Core API (.NET 8), as well as unit and integration test projects.

How to run MinimalApi

  1. Install .NET 8 SDK.
  2. Make sure you have MySQL installed and running on your machine, as it is the database configured for the application.
  3. Set the connection string in the ConnectionStrings:DefaultConnection key in appsettings.json (or via user secrets). Example (replace <username> and <password> with your MySQL credentials):
    "ConnectionStrings": {
      "DefaultConnection": "Server=localhost;Database=MinimalApiDb;User Id=<username>;Password=<password>;"
    }
  4. Set the JWT key in the Jwt:Key key in appsettings.json (or via user secrets). The JWT token uses the HS256 algorithm, so the key must be at least 32 characters long. A sample key is already provided for development:
    "Jwt": {
      "Key": "MySecretKeyForJWT123456789012345"
    }
  5. Make sure you are in the project root folder (where the .sln file is) so relative paths work correctly.
  6. Run:
    dotnet run --project MinimalApi/MinimalApi.csproj
  7. The application will run on the ports specified in the https:applicationUrl key in MinimalApi/Properties/launchSettings.json:

Unit Tests

  • Unit tests are in MinimalApi.UnitTests.
  • To run, be in the project root folder:
    dotnet test MinimalApi.UnitTests/MinimalApi.UnitTests.csproj

Integration Tests

  • Integration tests are in MinimalApi.IntegrationTests.
  • To run, be in the project root folder:
    dotnet test MinimalApi.IntegrationTests/MinimalApi.IntegrationTests.csproj
  • Important: Integration tests use Testcontainers to create a temporary MySQL database via Docker. Make sure Docker is installed and running. This avoids the need for a manual test database, as the container is created and destroyed automatically for each test run, creating a copy of the application database for testing.

About

Implementação de uma Minimal API com Entity framework e Testes Automatizados em .NET 8

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages