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

Possibility to disambiguate constructors when creating spies #1023

Open
Vampire opened this issue Sep 2, 2019 · 2 comments

Comments

@Vampire
Copy link
Contributor

commented Sep 2, 2019

Given you have a class with two constructors, one taking a String argument, one taking a Pattern argument.
Currently you cannot create a Spy with null as argument as far as I have seen.
The only way I found is to use Objenesis to create the spy and then set any (private) fields the constructor would have set manually.
In normal code you could do new MyClass(null as String) (which through Spock also does not work properly as described in #1022), but Spy(constructorArgs: [null as String]) does not work.
The cast is not carried on to where the constructor to invoke is searched and you get an ambiguous warning.

So I'd say we need either some typed-null, that can be used to find the right constructor and is then supplied as null to the constructor like Spy(constructorArgs: [new Null(String)]) or a second property where you can give the types like Spy(constructorArgs: [null], constructorTypes: [String]).

@Vampire

This comment has been minimized.

Copy link
Contributor Author

commented Sep 2, 2019

To clarify why I needed this, I test an abstract class and thought the nicest way would be to create a Spy of it.
To work-around the problem, I created a subclass of the testee where I forward the casted parameters.

@Vampire

This comment has been minimized.

Copy link
Contributor Author

commented Sep 4, 2019

Actually I guess the constructorTypes way would be better, because if you supply some variable that can be null you also need to explicitly disambiguate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.