FlexTradeUKLtd / jfixture

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

Support for project lombok @Data (and friends) #18

Closed mishamo closed 9 years ago

mishamo commented 9 years ago

Currently jfixture doesn't recognise the public constructor generated on a project lombok @Data object and thus cannot instantiate it as a fixture.

richkeenan commented 9 years ago

Hi @mishamo thanks for the report.

I've tried to reproduce this on V2.1.0 but the tests I've done work as expected with @Data for both immutable and mutable objects. For example a data object like,

@Data
public class ImmutableData {
    private final String aString;
    private final List<Integer> list;
}

with a unit test to verfiy

@Test
public void should_create_immutable_lombok_data_objects() {
    JFixture fixture = new JFixture();

    ImmutableData data = fixture.create(ImmutableData.class);

    assertThat(data, not(nullValue()));
    assertThat(data.getAString(),  not(nullValue()));
    assertThat(data.getList(), not(nullValue()));
    assertThat(data.getList().size(), greaterThan(0));
}

The test passes.

Are you able to provide a class using @Data that doesn't work for you? It's possible that it's an object JFixture can't make due to the constructor parameters rather than the use of @Data. Thanks!

mishamo commented 9 years ago

Aha! The issue isn't @Data at all, it's @Builder (when combined with @Data). The following throws an exception:

  @Test
  public void shouldNotThrowException() throws Exception {
    JFixture fixture = new JFixture();
    fixture.create(SomeData.class);
  }

  @Data
  @Builder
  public static class SomeData {
    private final String someString;
  }

Removing @Builder or delomboking @Data causes this to work as expected. Fails with the following stack trace:

java.lang.UnsupportedOperationException: JFixture was unable to create an instance from class com.something.SomeTest$SomeData, most likely because it has no public constructor, is an abstract or non-public type or has no static factory methods
        class com.something.SomeTest$SomeData --> 
        public com.something.SomeTest$SomeData(java.lang.String) --> 
        class com.something.SomeTest$SomeData

    at com.flextrade.jfixture.behaviours.noresolution.ThrowingNoResolutionHandler.handleNoResolution(ThrowingNoResolutionHandler.java:9)
    at com.flextrade.jfixture.behaviours.noresolution.NoResolutionGuard.handleNoResolutionForRequest(NoResolutionGuard.java:52)
    at com.flextrade.jfixture.behaviours.noresolution.NoResolutionGuard.create(NoResolutionGuard.java:29)
    at com.flextrade.jfixture.JFixture.create(JFixture.java:83)
    at com.flextrade.jfixture.JFixture.create(JFixture.java:78)
    at com.something.SomeTest.shouldNotThrowException(SomeTest.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
mishamo commented 9 years ago

Can resolve this as a bug in project lombok: https://code.google.com/p/projectlombok/issues/detail?id=657