Unit test of implementations of a Generic class

Asked

Viewed 1,107 times

4

Suppose we have the following interface:

public interface ICRUDService<T>
{
    T Create(T entity);            
}

Abstract implementation:

public abstract class CrudService<T> : ICRUDService<T>        
{         
    IRepository repositorio;      
    public EntityService(IRepository rep)
    {
        repositorio= rep;            
    }  

    public virtual T Create(T entity)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity ");
        }
        repositorio.Add(instance);           
        return entity;
    }                  
}

For each object in my domain (Ex: Person, Car, Animal) will implement a service:

  public class PessoaService :CrudService<Model.Pessoa>{
    IRepository repositorio;      
    public PessoaService (IRepository repo):base(repo)
    {
        repositorio= repo;
    }
    public void MetodoPersonalizadoServicoPessoa(){
         var oi = "oi";
    }         
   } 

  public class CarroService :CrudService<Model.Carro>{
    IRepository repositorio;      
    public CarroService (IRepository repo):base(repo)
    {
        repositorio= repo;
    }
    public void MetodoPersonalizadoServicoCarro(){
         var oi = "oi2";
    }         
   }

-How best to test each Crudservice implementation?

-For each implementation I must repeat the Create method test of the abstract class?

-Is there any way to create a generic Abstract test that automatically tests the Create method in all classes that implement it?

1 answer

3

How best to test each Crudservice implementation?

Creating an explicit test for each implementation.

You can test the virtual methods of the abstract class by creating in the test code an inheritance of this class and testing this inherited class. It is important to do this to ensure that virtual methods not superscripted by any concrete implementation are also tested.

For each implementation I must repeat the Create method test of the abstract class?

You must repeat the test only for concrete implementations that override the virtual method of the abstract parent class.

No need to repeat the test when the virtual method is not overwritten or you will be testing the same code twice.

Is there any way to create a generic Abstract test that automatically tests the Create method in all classes that implement it?

If you refer to using some magic type Reflection to identify all concrete implementations and get out testing... Not a good idea. Each test should be expressive, indicating very clearly what is being tested and for what purpose.

If you use Reflection to identify what to test, there may come a time when this search will find nothing and the test will continue there "soiling" your test suite and eventually causing confusion of the type: CRUD services seem to be being tested and are not.

Inheritance and testing

Avoid using inheritance only for code reuse.

Note, by the amount of doubt that inheritance usage has raised, that inheritance adds quite complexity and breaks abstraction (see that you need to know if a class overwrites or not a parent class method to know how to test it or to avoid repeated testing).

The use of inheritance, in your example, is not bringing any benefit.

  • How should I reuse code without using inheritance? Utility classes?

  • 1

    @Leocbs Exactly. The simplest form of reuse is to encapsulate the code in an independent class and consume this class. In the case of this question, CrudService could be simply a utility class instead of being a parent class. Consumption of it could be more or less like this: CrudService.Create<TipoEntidade>(repo, entidade). The names are probably wrong - create doesn’t seem to be creating anything, but that’s another matter.

  • In which cases it is recommended to use inheritance?

  • @Leocbs Hence is an extensive subject. My standard procedure is not to use class inheritance. I avoid using especially when the code deals with business irrigation and am less rigid when it comes to simple and more generic technology accessory solutions. I don’t know if you already have a question like that here in the OS and I don’t even know if it would be off topic, but you can do a search and ask a question if that’s the case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.