Integration Testing in ASP.NET Core – API Contract Testing

Integration Testing in ASPNET Core series

Today in this article, we will cover Integration Testing in ASP.NET Core.

In our previous article, we looked at how to perform an Integration Test between the Target Service we were developing (Software Under Test) and downstream components which was a Database.

We will cover below aspects,

In real scenarios, your downstream component could be an API/Service itself. In such a situation one can perform contract testing along with other connectivity (if downstream or Upstream service is an API/Service.)

IntegrationTest- Contract Testing

Let’s take an example below where Service A is communicating with Service B along with Database as MongoDB.

Contract Testing

In our last article by doing IntegrationTesting, we validated the downstream connectivity of service by verifying the connection with the MongoDB database.

However, Service A gonna be possibly consumed by some other Component or Service.

In such scenarios, it’s going to be useful if upstream connectivity is also validated.

Such testing very much resembles contract Testing.

In the example, Service A returns a model Employee entity that is consumed by ServiceB.

For example, Le’s assume this handshake is in the form of an Employee Model entity which is shared as JSON between these two services.

Our Integration Test would intend to verify the Employee schema.

Integration Testing in ASPNET Core series

This will validate that Service A and Service B are following the contract.

Here is the schema,

public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string ZipCode { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
        public string EmployeeId { get; set; }
    }

Let’s update the Test scenarios which we learned in our previous article,

public class BooksServiceIntegrationTests : IClassFixture<AppTestFixture>
    {
        readonly AppTestFixture _fixture;
        readonly HttpClient _client;
        public BooksServiceIntegrationTests(AppTestFixture fixture)
        {
            _fixture = fixture;
            _client = _fixture.CreateClient();
        }

        [Fact]
        public async Task GET_GetEmployeeDetails_Valid_Input_Success()
        {
            //Arrange
            string employeeNumber = "1234";

            // Act
            var response = await _client.GetAsync($"/api/employee/{employeeNumber}");
            var employeeResponse = JsonConvert.DeserializeObject<Employee>(response.Content.ReadAsStringAsync().Result);

            // Assert1
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            // Assert2
            Assert.NotNull(employeeResponse);

            // Assert3
            Assert.IsType<Employee>(employeeResponse);
        }
...
..
}
Integration Testing in ASPNET Core series

Above, finally, we are achieving below,

  • Making sure Database connections are established.
  • We are also making sure the service returns the proper HTTP Status code to its consumer
  • We are making sure Service Under Development is going to return from Entity as per Consumerscontract.

References:

That’s All. Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *