---
title: "How to Integrate Unit Testing with ASP.NET Core 3.1"
published_at: "2020-04-30T10:30:29+00:00"
modified_at: "2023-12-27T13:24:04+00:00"
url: "https://www.syncfusion.com/blogs/post/how-to-integrate-unit-testing-with-asp-net-core-3-1"
excerpt: "In this blog, we are going to walk through the steps to integrate unit testing for ASP.NET Core 3.1 projects. This includes implementing sample business logic, creating mock objects for database calling using Moq, and verifying the method call using..."
taxonomy_category:
  - ".NET Core"
  - "ASP.NET Core"
taxonomy_post_tag:
  - ".NET Core"
  - "ASP.NET Core"
  - "Unit Test"
  - "unit testing"
---

# How to Integrate Unit Testing with ASP.NET Core 3.1

[Vijayaraj Angappan](https://www.syncfusion.com/blogs/author/vijayaraj-angappan)

![How to Integrate unit testing with ASP.Net Core 3.1](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/How-to-Integrate-unit-testing-with-ASP.Net-Core-3.1.jpg)


In this blog, we are going to walk through the steps to integrate unit testing for **ASP.NET Core 3.1** projects. This includes implementing sample business logic, creating mock objects for database calling using *** Moq***, and verifying the method call using *** NUnit*** asserts.

## What is unit testing?

Unit testing is a level of software testing that is implemented and executed by software developers to ensure implemented logic meets its requirement and behaves as expected.

## Purposes of unit testing

The purposes of unit testing are:

- To segregate and test that each part of a program is working as expected.
- To increase confidence while modifying logic when maintaining code.
- To easily identify the defects in an application since unit testing data is closed to production.
- To verify the accuracy of each program unit.

## Choosing the right framework

Based on the selected implementation language need to find the tool or framework for unit testing. Here we have chosen ***NUnit*** and *** Moq*** to implement test methods.

### NUnit

NUnit is one of the most widely used open-source testing tools for **.NET Framework**. It supports building static asserts to ensure testing results. It has a console runner, which is used for batch execution of test cases.

### **Setup method**

The**Setup** method will be called once, before the execution of all test cases within a test class. This method arranges common mocking logic for all test methods.

The following image is an example.![Setup method](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/01-Setup-method.png)

### **Test attribute**

The **Test** attribute to a method indicates that it should be run by the Test Runner application when there is a call to run test cases. The following are the criteria for adding the Test attribute to a method:

- The method must be declared public.
- The method’s return type should be void.

The following image is an example.![Test Attribute](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/02-Test-Attribute.png)

### Moq

A mock object allows us to copy or clone the behavior of a class and interface. This is used when interacting with test cases.

With the use of mocks, we can set up the object, including setting parameters and return values on the function calls. Also, we can verify that the methods we set up are being called during the execution of the tested cases.

Refer to this [Moq example](https://github.com/Moq/moq4/wiki/Quickstart)
 on GitHub.

## Create a sample ASP.NET Core application

Follow these steps to create a .NET Core project in **Visual Studio 2019**:

**Step 1**: In Visual Studio 2019, select ** File** -> ** New** -> ** Project**.

**Step 2**: In the Create New Project dialog, select the ** ASP.NET Core Web Application** template and click ** Next**.![Create a new project dialog](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/03-Create-a-new-project-dialog.png)

**Step 3**: Enter the project name, and then click ** Create**. The Project template dialog will be displayed.

**Step 4**: Select **.NET Core**,** ASP.NET Core 3.1**, and the ** API** template (as shown in the following screenshot).![Create a new ASP.NET Core web application](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/04-Create-a-new-ASP.NET-Core-web-application.png)

## Create unit test project with Moq and NUnit integrated

Follow these steps to add the test project:

**Step 1**: Right-click on the project solution and select the ** Add -> New Project**option.![Add a new project](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/05-Add-a-new-project.png)

**Step 2**: Select ** NUnit test project (.NET Core)** and click ** Next**.![NUnit test project](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/06-NUnit-test-project.png)

**Step 3**: Enter the project name, and then click ** Create**. The created project will be displayed like the following.![Solution Explorer](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/07-Solution-Explorer.jpg)

**Step 4**: Open *** NuGet*** manager for ** Project1.Tests** and search with keyword *** Moq***. The result will be displayed like in the following screenshot. Select ** Moq** and o install it.![NuGet pack manager](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/08-NuGet-pack-manager.jpg)

**Step 5:** Ensure that the NuGet package is installed.![Installing Moq NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/09-Installing-Moq-NuGet-package.jpg)

## Create your first, super simple unit test case

We are going to write a test case for a method that returns an addition of two numbers.

**Step 1:** Add the class file ** MathBL.cs** with the method ** Sum()**in** Project.Main.Test**.![Declaring a class to test](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/10-Declaring-a-class-to-test.png)

**Step 2**: Select the ** Project1.Test** project and add class file ** MathTestBL.cs**.

**Step 3**: Add the methods ** Setup()** and ** TestSum()** inside the MathTestBL.cs class.![Adding a test method](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/11-Adding-a-test-method.png)

**Step 4**: Right-click on ** Project1.Test** project dependencies and add the project reference ** Project.Main.Test**

**Step 5**: Write the following code to call the ** Sum()** method.![Defining the test method](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/12-Defining-the-test-method.png)

Sample code:

**Step 6**: Build the project and ensure the build succeeds**.** Then select ** View -> Test Explorer**.![Opening test explorer](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/13-Opening-test-explorer.png)

**Steps 7**: You will see the list of test methods. Right-click on ** MathTest.BL** and then click ** Run**.![Running the test](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/14-Running-the-test.png)![Test Result](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/15-Test-Result.png)

The test run is successful, and the test case passed. You can alter the test result to seven or so to see if it fails.

## Unit test case for mock database and data access service call

### Implement business logic and database context

We have followed a code-first approach and created a database context with the modal class **Customer.cs**.

Take a look at the following, where the database context and business logic already exist.

### **Modal class and database context**

**![Customer model class](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/16-Customer-model-class.png)![Customer db context](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/17-Customer-db-context.png)**

### **Data access logic**

The following image demonstrates implemented generic data access logic (**EntityRepository.cs**) with an interface (** IEntityRepository.cs**).![Entity repository](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/18-Entity-repository.png)![Entity repository interface](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/19-Entity-repository-interface.png)

### **Business logic**

I created **CustomerBL.cs** class with the following methods:

- GetActiveCustomer()
- SaveCustomer()

![Customer BL class](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/20-Customer-BL-class.png)This is the sample source code (CustomerBL.cs).

### Unit test for business logic

I created **CustomerTestBL.cs** and implemented ** TestGetActiveCustomers**() test method like in the following image.![Unit test case for Customer Business Logic class](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/21-Unit-test-case-for-Customer-Business-Logic-class-2.jpg)

### Mock and test asserts

The following steps have been implemented:

1. Mock instance was created for **IEntityRepository<Customer>**.
2. Setup has been configured to return a defined static customer list. Now whenever the repository method **GetQueriable()** is invoked, a static customer list will be returned.
3. The test case is verified using assert checking after the method call result.![Mock and test](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/22-Mock-and-test.png)

### Run test

**Step 1**: Select ** View -> Test Explorer**. This will display the list of test case methods.

**Step 2**: Right-click on the ** TestGetActiveCustomer()** method and click ** Run**.![Running the test cases](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/23-Running-the-test-cases-2.jpg)

**Step 3**: We can see the successful execution of the test case result.![Test result](https://www.syncfusion.com/blogs/wp-content/uploads/2020/04/24-Test-result-2.jpg)

## Conclusion

In this blog, we have learned how to integrate a test project with a .NET Core application. Also, we learned about ***Mock*** method calls and the verification of the returned result using *** NUnit*** asserts. I hope you found this blog useful.

Syncfusion provides 70+ high-performance, lightweight, modular, and responsive [ASP.NET Core UI controls](https://www.syncfusion.com/aspnet-core-ui-controls)
 such as [DataGrid](https://www.syncfusion.com/aspnet-core-ui-controls/grid)
, [Charts](https://www.syncfusion.com/aspnet-core-ui-controls/charts)
, and [Scheduler](https://www.syncfusion.com/aspnet-core-ui-controls/scheduler)
. You can use them to improve your application development.

Please provide your feedback in the comments section. You can also contact us through our [support forum](https://www.syncfusion.com/forums)
, [support portal](https://support.syncfusion.com/)
, or [feedback portal](https://www.syncfusion.com/feedback)
. As always, we are happy to assist you!

## Related blogs

- [Easily Convert Organizational Chart Diagrams to PowerPoint Presentations](https://www.syncfusion.com/blogs/post/org-chart-diagram-to-ppt.aspx)
- [Simple Steps to View a PowerPoint Presentation in an ASP.NET Core Application](https://www.syncfusion.com/blogs/post/view-powerpoint-asp-dotnet-core.aspx)
- [Easily Group Data into Ranges in Web Applications Using Pivot Table](https://www.syncfusion.com/blogs/post/easily-group-data-into-ranges-in-web-applications-using-pivot-table.aspx)
- [Handling CSV Files in ASP.NET Core Web APIs](https://www.syncfusion.com/blogs/post/handling-csv-files-in-asp-net-core-web-apis.aspx)
