Skip to content

Issue with Mockito and JpaRepository #12467

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
YLombardi opened this issue Mar 13, 2018 · 5 comments
Closed

Issue with Mockito and JpaRepository #12467

YLombardi opened this issue Mar 13, 2018 · 5 comments
Labels
status: duplicate A duplicate of another issue

Comments

@YLombardi
Copy link

Hello,

I don't know if this an issue with Spring boot data JPA or with Mockito but I encountered a strange behaviour.
I create 2 Controller. In the first, I inject a Service. In the second, I inject a JpaRepository.

Now I create UnitTests for both this Controller.
For the second Controller, I mock the Repository. Everything works fine.
For the first Controller, I mock the Service. The test fail.
It seems to search to inject the Repository whereas it is not used in this Controller.

I create a very simple project to reproduce this behavior :
https://github.com/YLombardi/mockito-spring-jpa-issue

Is it my test that is wrong or a bug ?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 13, 2018
@wilkinsona
Copy link
Member

Thanks for the sample, @YLombardi. This looks like a bug to me. I'd expect your ControllerAUnitTests to have filtered out ControllerB due to your configuration of @WebMvcTest specifying a single controller:

@WebMvcTest(ControllerA.class)

@wilkinsona wilkinsona added priority: normal and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 16, 2018
@wilkinsona wilkinsona added this to the 1.5.11 milestone Mar 16, 2018
@wilkinsona wilkinsona added the type: bug A general bug label Mar 16, 2018
@bclozel
Copy link
Member

bclozel commented Mar 16, 2018

The main application class has a @ComponentScan(basePackages = "com.geodis.rt"); removing it solves the issue. Also, the test has a typo where it's checking for a view .andExpect(view().name("aview")); this line should be removed.

@wilkinsona
Copy link
Member

Thanks, @bclozel. I was wondering why our tests for this were passing and hadn't spotted that difference . I wonder if there's something we can do to at least make it obvious that the use of @ComponentScan, rather than the aliased attributes on @SpringBootApplication, is going to cause a problem.

Either way, I think we should update the javadoc as it currently says that @SpringBootApplication is "a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration, and @ComponentScan".

@wilkinsona
Copy link
Member

Now that I know what's going on, this is a duplicate of #10933.

@wilkinsona wilkinsona removed this from the 1.5.11 milestone Mar 16, 2018
@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed priority: normal type: bug A general bug labels Mar 16, 2018
@YLombardi
Copy link
Author

YLombardi commented Mar 19, 2018

Thanks for your answers.
I will see if I can remove the @ComponentScan in my main application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

4 participants