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

add constructor assertions for ClassAssert #1693

Open
xenoterracide opened this issue Nov 7, 2019 · 15 comments
Open

add constructor assertions for ClassAssert #1693

xenoterracide opened this issue Nov 7, 2019 · 15 comments

Comments

@xenoterracide
Copy link

xenoterracide commented Nov 7, 2019

Example

assertThat(MyClass.class).hasPackageOrBetterNoArgConstructor(); // common for frameworks
assertThat(MyClass.class).hasPublicConstructor(Integer.class, String.class); // common for published api's

I'm not fussy on names

@joel-costigliola
Copy link
Member

I would likely split hasPackageOrBetterNoArgConstructor into hasPackage and hasPackagePrivateConstructor and add hasProtectedConstructor

@xenoterracide
Copy link
Author

that's probably fine, hmm could have hasBeanConstructor, but yeah if you prefer splitting it I'm ok with that.

@wettera
Copy link

wettera commented Apr 26, 2020

Does this issue mean that we need to test whether a class have a private or protected or public constructor? I want to fix this issue
For example:
assertThat(MyClass.class).hasPrivateConstructor
assertThat(MyClass.class).hasProtectedConstructor
assertThat(MyClass.class).hasPublicConstructor
@xenoterracide @joel-costigliola

@wettera
Copy link

wettera commented Apr 26, 2020

sorry......
forget the brackets and semicolon after each asseration.

@scordio
Copy link
Member

scordio commented Apr 26, 2020

Thinking out loud, would it make sense to introduce assertions for java.lang.reflect.Constructor?

And AbstractClassAssert would have a new getConstructor(Class<?>...) method.

Something like:

assertThat(MyClass.class).getConstructor()  // default constructor
                         .isPublic();
assertThat(MyClass.class).getConstructor(Integer.class, String.class)
                         .isPackagePrivate();

@wettera
Copy link

wettera commented Apr 26, 2020

But one class could have multiple constructors, these constructors may be private, protected, or public.

@wettera
Copy link

wettera commented Apr 26, 2020

.getConstructor() may get a constructor list. Maybe in the list one constructor is parvate, another contructor is public.

@scordio
Copy link
Member

scordio commented Apr 26, 2020

But one class could have multiple constructors, these constructors may be private, protected, or public.

The getConstructor(Class<?>...) I proposed would have Class#getDeclaredConstructor(Class<?>...) behind, where the resulting constructor depends on the parameter types given as input, so only one constructor at a time would be returned.

This is actually a second step. It would be enough to have ConstructorAssert which provides assertions on the modifiers. Something like this could be written:

assertThat(MyClass.class.getDeclaredConstructor()).isPublic();

@wettera
Copy link

wettera commented Apr 26, 2020

But one class could have multiple constructors, these constructors may be private, protected, or public.

The getConstructor(Class<?>...) I proposed would have Class#getDeclaredConstructor(Class<?>...) behind, where the resulting constructor depends on the parameter types given as input, so only one constructor at a time would be returned.

This is actually a second step. It would be enough to have ConstructorAssert which provides assertions on the modifiers. Something like this could be written:

assertThat(MyClass.class.getDeclaredConstructor()).isPublic();

Both MyClass.class.getDeclaredConstructors() and MyClass.class.getConstructors() parameter list is empty. I checked just now.

@wettera
Copy link

wettera commented Apr 26, 2020

But one class could have multiple constructors, these constructors may be private, protected, or public.

The getConstructor(Class<?>...) I proposed would have Class#getDeclaredConstructor(Class<?>...) behind, where the resulting constructor depends on the parameter types given as input, so only one constructor at a time would be returned.

This is actually a second step. It would be enough to have ConstructorAssert which provides assertions on the modifiers. Something like this could be written:

assertThat(MyClass.class.getDeclaredConstructor()).isPublic();

Sorry I make a mistake, instance.getClass().getDeclaredConstructors() and instance.getClass().getConstructors() parameter list is empty.

@wettera
Copy link

wettera commented Apr 26, 2020

But one class could have multiple constructors, these constructors may be private, protected, or public.

The getConstructor(Class<?>...) I proposed would have Class#getDeclaredConstructor(Class<?>...) behind, where the resulting constructor depends on the parameter types given as input, so only one constructor at a time would be returned.

This is actually a second step. It would be enough to have ConstructorAssert which provides assertions on the modifiers. Something like this could be written:

assertThat(MyClass.class.getDeclaredConstructor()).isPublic();

Sorry.....
MyClass.class.getDeclaredConstructors()
MyClass.class.getConstructors()
instance.getClass().getDeclaredConstructors()
instance.getClass().getDeclaredConstructors()
can have parameter type as input and return one unique constructor.
I'm sorry I was so careless.

@scordio
Copy link
Member

scordio commented May 4, 2020

@xenoterracide @wettera we discussed the topic internally and we would prefer a separate ConstructorAssert to avoid bloating the ClassAssert API. Do you see any issue with it?

@xenoterracide
Copy link
Author

nope, though you might consider allowing chaining from the class assert, that way it's easily discoverable, but I'll leave that up to you. Since my biggest priority was to ensure that I had a public noargs constructor.

@scordio
Copy link
Member

scordio commented May 4, 2020

you might consider allowing chaining from the class assert

Yes, chaining is definitely in the roadmap

@JitendraProgrammer
Copy link

Hi Guys,

Can you guide me how can i contribute here

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

Successfully merging a pull request may close this issue.

5 participants