Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creation of a new instance of generic type parameter not getting code coverage

I've run my code through code coverage and the line below shows 1 block as not covered.

Can anyone tell me which part of that line isn't executing?

enter image description here

An Example to play with:

public abstract class Base
{
    public abstract IExample CreateEntity<TExample>() where TExample : IExample, new();
}

public class Class1 : Base
{
    public override IExample CreateEntity<TExample>()
    {
        IExample temp = new TExample();
        return temp;
    }
}

public interface IExample
{

}

public class TEx : IExample
{

}

and the test method

    [TestMethod]
    public void TestMethod1()
    {
        Class1 ex = new Class1();
        ex.CreateEntity<TEx>();
    }
like image 412
James Barrass Avatar asked Aug 15 '13 16:08

James Barrass


People also ask

Can you create instances of generic type parameters?

A generic type is like a template. You cannot create instances of it unless you specify real types for its generic type parameters. To do this at run time, using reflection, requires the MakeGenericType method.

How do I get a class instance of generic type T?

The short answer is, that there is no way to find out the runtime type of generic type parameters in Java. A solution to this is to pass the Class of the type parameter into the constructor of the generic type, e.g.

How can we create object for generic class?

Like C++, we use <> to specify parameter types in generic class creation. To create objects of a generic class, we use the following syntax. Note: In Parameter type we can not use primitives like 'int','char' or 'double'.

How do you create a generic variable in Java?

A Generic Version of the Box Class To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.


1 Answers

Change your constraint to force the TExample to be a class:

public abstract IExample CreateEntity<TExample>() where TExample : class, IExample, new();

If you run your compiled code through a tool like ILSpy, you will see the block that is not getting coverage:

TExample temp = (default(TExample) == null) ? Activator.CreateInstance<TExample>() : default(TExample);
return temp;

It is performing a check to see if the type passed to the generic was a reference type or a value type. By forcing it to be a class, this check will be removed. Read more on the default keyword here: http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx

Another way to get complete code coverage would be to use a struct that implements IExample:

public struct S1 : IExample
{ 
}

And then add this test:

[TestMethod]
public void StructTest()
{
    Class1 ex = new Class1();
    ex.CreateEntity<S1>();
}
like image 117
John Koerner Avatar answered Oct 05 '22 23:10

John Koerner