Skip to content

ConfigurationProperties with constructor binding cannot be mocked #18652

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

Closed
sdeleuze opened this issue Oct 18, 2019 · 9 comments
Closed

ConfigurationProperties with constructor binding cannot be mocked #18652

sdeleuze opened this issue Oct 18, 2019 · 9 comments
Labels
type: bug A general bug
Milestone

Comments

@sdeleuze
Copy link
Contributor

sdeleuze commented Oct 18, 2019

I have migrated https://spring.io/guides/tutorials/spring-boot-kotlin/ to Spring Boot 2.2.0.RELEASE and updated it to leverage immutable configuration properties. As discussed with @snicoll, @ConfigurationProperties are not scanned by default in test slices, so I have to add to my @WebMvcTest test either @EnableConfigurationProperties(BlogProperties::class) or mock BlogProperties bean.

My test works with @EnableConfigurationProperties(BlogProperties::class) but fails if I try to mockk BlogProperties bean (in my case with @MockkBean from https://github.com/Ninja-Squad/springmockk) with following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.blog.BlogProperties#0': @EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type com.example.blog.BlogProperties
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 18, 2019
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 22, 2019
@snicoll snicoll added this to the 2.2.1 milestone Oct 22, 2019
@snicoll snicoll changed the title Error when mocking immutable configuration properties ConfigurationProperties with constructor binding cannot be mocked Oct 22, 2019
@snicoll
Copy link
Member

snicoll commented Oct 23, 2019

Interesting data point is that it only fails if @ConstructorBinding is set on the type. If the annotation is set on the constructor, then the error goes away. I have a hunch that this is directly related to #18685

@snicoll
Copy link
Member

snicoll commented Oct 23, 2019

And I was wrong. It works because the mock does not expose the annotations that are available on the target constructor so the current validation does not apply.

@philwebb philwebb modified the milestones: 2.2.1, 2.2.x Oct 23, 2019
@philwebb
Copy link
Member

@sdeleuze Have you got a branch somewhere we can look at?

@snicoll
Copy link
Member

snicoll commented Oct 24, 2019

@philwebb that wasn't obvious from my previous comment but that's not Kotlin specific as far as I can see. I didn't try with Kotlin so can't say but that's what seems to happen with plain Java, see https://github.com/snicoll-scratches/test-mock-config-props

If the annotation is found in both cases, it will fail in both cases whereas this issue is about it not failing at all. If a bean is mocked, the verification should not check for the bind method anyway.

@sdeleuze
Copy link
Contributor Author

@philwebb Sadly not anymore, it was on my laptop which was stolen last week.

@philwebb
Copy link
Member

:(

@davinkevin
Copy link
Contributor

davinkevin commented Oct 26, 2019

Same thing for me with mockito btw (in kotlin too, not tested in java).

https://gitlab.com/davinkevin/Podcast-Server/issues/37

davinkevin added a commit to davinkevin/Podcast-Server that referenced this issue Oct 29, 2019
Configuration properties have not been merged due to error in spring-projects/spring-boot#18652

Closes #35
@mbhave mbhave closed this as completed in 471ca01 Nov 6, 2019
@mbhave
Copy link
Contributor

mbhave commented Nov 6, 2019

@sdeleuze @davinkevin I've pushed a fix for this. Could you verify if this works for you? Thanks.

@mbhave mbhave modified the milestones: 2.2.x, 2.2.1 Nov 6, 2019
@snicoll
Copy link
Member

snicoll commented Nov 6, 2019

@mbhave the sample I built for this now works with master. Thanks!

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

No branches or pull requests

6 participants