Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to code unit tests for every single layer on an n-tier architecture?

I'm new to Unit Tests so I've been trying to code some examples to learn the right way to use them. I have an example project which uses Entity Framework to connect to a database.

I'm using an n-tier architecture composed by a data access layer which queries the database using EF, a business layer which invokes data access layer methods to query database and perform its business purpose with the data retrieved and a service layer which is composed of WCF services that simply invoke business layer objects.

Do I have to code unit tests for every single layer (data access, business layer, services layer?

Which would be the right way to code a unit test for a method that queries a database? The next code is an example of a method in my data access layer which performs a select on the database, how should its unit test be like?

public class DLEmployee
{

    private string _strErrorMessage = string.Empty;
    private bool _blnResult = true;

    public string strErrorMessage
    {
        get
        {
            return _strErrorMessage;
        }
    }
    public bool blnResult
    {
        get
        {
            return _blnResult;
        }
    }

    public Employee GetEmployee(int pintId)
    {
        Employee employee = null;
        _blnResult = true;
        _strErrorMessage = string.Empty;

        try
        {
            using (var context = new AdventureWorks2012Entities())
            {
                employee = context.Employees.Where(e => e.BusinessEntityID == pintId).FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            _strErrorMessage = ex.Message;
            _blnResult = false;

        }

        return employee;
    }
like image 293
razp26 Avatar asked Aug 10 '15 16:08

razp26


People also ask

What tasks are carried out by the application layer of an N-tier architecture?

N-tier application architecture provides a model by which developers can create flexible and reusable applications. By segregating an application into tiers, developers acquire the option of modifying or adding a specific tier, instead of reworking the entire application.

What is the main difference between an n-tier and an N layer layered architecture?

N-tiered refers to the "distributed" layers of a system (i.e. server and client), whereas n-layered refers to the layers in a self-contained program; although the two are often used interchangeably, some suggest that there is a significant difference (like the one I mentioned above), as seen on the first paragraphs on ...

What is the difference between 3 tier and N-tier architecture?

In 3 Tier Application there are three tiers like Presentation Layer , Application Layer and Data layer. Here the application layer contains business logic as well . On the other hand in N Tier Application layer is divided into 2 i.e. Application Layer and Business Logic layer.

When Should unit testing be performed?

Unit testing is the first testing phase and it is practiced before moving to the phase of integration testing. Hence, before moving for the next testing level, make sure to fix all the identified bugs in the unit testing phase.


1 Answers

Here are my 2 cents based on Domain Driven Design principles:

  • Your business layer should not depend on the concrete data layer, it should just define some abstract interfaces that the data layer can implement (repositories).
  • You should definitely unit-test your business layer with a fake data layer that does not touch the file system.
  • You might create integration tests including your service and business layer with a fake data layer. There is no point mocking out the business layer and check what the service layer calls on the business layer (behavior testing), rather check what state changes it makes on the business objects that are observable through the business layer.
  • You should create some end-to-end tests with a real data layer, the service and business layer and exercise some use-cases on the service layer.

If you just started on unit testing, I advise you to read Kent Beck's Test Driven Development by Example and Gerard Meszaros' xUnit Test Patterns

like image 144
wigy Avatar answered Sep 27 '22 15:09

wigy