FlexTradeUKLtd / jfixture

JFixture is an open source library based on the popular .NET library, AutoFixture
MIT License
105 stars 22 forks source link

NPE when creating type that extends generic type #36

Closed nayan92 closed 7 years ago

nayan92 commented 7 years ago
public class TestClass {

    public class GenericClass<T> {
        private T field;

        public T getField() { return this.field; }

        public void setField(T field) { this.field = field; }
    }

    public class NonGenericClass extends GenericClass<String> { }

    public static void main(String[] args) {
        new JFixture().create(NonGenericClass.class);
    }

}

This example throws NPE because it can't figure out the actual type for T when attempting to fixture field

Exception in thread "main" java.lang.NullPointerException
    at com.flextrade.jfixture.utility.SpecimenType.<init>(SpecimenType.java:37)
    at com.flextrade.jfixture.utility.SpecimenType.<init>(SpecimenType.java:12)
    at com.flextrade.jfixture.utility.SpecimenType$3.<init>(SpecimenType.java:55)
    at com.flextrade.jfixture.utility.SpecimenType.withGenericContext(SpecimenType.java:55)
    at com.flextrade.jfixture.utility.ParameterUtils.convertPossibleGenericTypeToSpecimenType(ParameterUtils.java:50)
    at com.flextrade.jfixture.builders.GenericMethodBuilder.getMethodReturnType(GenericMethodBuilder.java:36)
    at com.flextrade.jfixture.builders.GenericMethodBuilder.create(GenericMethodBuilder.java:27)
    at com.flextrade.jfixture.builders.CompositeBuilder.create(CompositeBuilder.java:26)
    at com.flextrade.jfixture.behaviours.autoproperty.AutoPropertyBuilder.create(AutoPropertyBuilder.java:22)
    at com.flextrade.jfixture.behaviours.recursion.RecursionGuard.create(RecursionGuard.java:33)
    at com.flextrade.jfixture.behaviours.noresolution.NoResolutionGuard.create(NoResolutionGuard.java:27)
    at com.flextrade.jfixture.behaviours.specimentype.SpecimenTypeInjector.create(SpecimenTypeInjector.java:26)
    at com.flextrade.jfixture.SpecimenBuilderContext.resolve(SpecimenBuilderContext.java:13)
    at com.flextrade.jfixture.behaviours.autoproperty.AutoPropertyPopulater.populateSetters(AutoPropertyPopulater.java:34)
    at com.flextrade.jfixture.behaviours.autoproperty.AutoPropertyPopulater.execute(AutoPropertyPopulater.java:23)
    at com.flextrade.jfixture.behaviours.autoproperty.AutoPropertyBuilder.create(AutoPropertyBuilder.java:25)
    at com.flextrade.jfixture.behaviours.recursion.RecursionGuard.create(RecursionGuard.java:33)
    at com.flextrade.jfixture.behaviours.noresolution.NoResolutionGuard.create(NoResolutionGuard.java:27)
    at com.flextrade.jfixture.behaviours.specimentype.SpecimenTypeInjector.create(SpecimenTypeInjector.java:26)
    at com.flextrade.jfixture.JFixture.create(JFixture.java:82)
    at com.flextrade.jfixture.JFixture.create(JFixture.java:78)
cliffsun91 commented 7 years ago

This should now be supported (to a basic level) in release version 2.7.2