Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child class non-empty constructors not found #113

Open
tikal opened this issue Nov 12, 2019 · 3 comments
Open

Child class non-empty constructors not found #113

tikal opened this issue Nov 12, 2019 · 3 comments

Comments

@tikal
Copy link

tikal commented Nov 12, 2019

I was using fixtures with a class with final fields and constructors only, without issue.
I had to change this class to an abstract class eventually, but fixtures were not working on the subclasses, throwing Could not find constructor with args [] on class on the child class.
While there was no need for an empty constructor on the parent class.

Workaround: add an empty constructor on the parent class, override it with a private constructor on the child class.

public abstract class Transaction<T, U> {
    private final String transactionId;

   ...

    // Needed this to support fixtures
    TransactionHistory() {
        this(null);
    }

    TransactionHistory(final String transactionId) {
        this(transactionId);
    }
}

public class ChargeTransaction extends Transaction<ChargeRequest, ChargeResponse> {
    private ChargeTransaction() {
    }
}
@tikal
Copy link
Author

tikal commented Nov 13, 2019

Hum, I thing this is due to missing field rule on a base template. I guess every template should follow either constructor or instead should have a default empty constructor. Anyway, will reopen with more details if that is not the root cause, but I guess behavior is correct.

@tikal tikal closed this as completed Nov 13, 2019
@tikal tikal reopened this Nov 14, 2019
@tikal
Copy link
Author

tikal commented Nov 14, 2019

Actually the error still stands. Even with rules matching child constructor.

@douglasmiguel7
Copy link

Hello @tikal and developers!

I guess I found a way to fix it.
Recently I get the same error when trying to run code below:

The Test.class:

public class Test {

    private final String name;
    private final  String lastname;
    private final Test other;

    public Test(String name, String lastname, Test other) {
            this.name = name;
            this.lastname = lastname;
            this.other = other;
        }

}

The fixture:

Fixture.of(Test.class).addTemplate(
    "default",
    new Rule() {{
        add("name", "Doug");
        add("lastname", "Andrade");
        add("other", null);
    }}
);

But, when I changed the line br/com/six2six/fixturefactory/util/ReflectionUtils.java:110 to:

return new Mirror().on(target).reflect().constructor().withAnyArgs().newInstance(parameters.toArray());

Works fine when a provided value is null or not null.
I found this solution on Mirror Docs

Probably this code works finer, but first we need to get constructor arguments types:

return new Mirror().on(target).reflect().constructor().withArgs(argTypes.toArray()).newInstance(parameters.toArray());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants