-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
@MockBean not Working with SpringBootTest and Junit5 #2654
Comments
Could you sprinkle some breakpoints in the I would expect something goes wrong when |
Hello @mpkorstanje , thank you for prompt response ! I was already debugging and it turns out the spring context gets properly populated with the mocked beans. I put a breakpoint in my @BeforeAll and did inject the Spring ApplicationContext then did in evaluate mode "context.getBean(EmailChannelAdapter.class)" and it returns the mock. This means that the problem is in the injection of the beans. After following your advice: I found out that inside the following method in the MockitoTestExecutionListenr, if I set the property REINJECT_DEPNDENCIES_ATTRIBUTES to true - then everything works fine.
Any help is greatly appreciated :) |
Sounds like #2572, specifically we don't invoke cucumber-jvm/cucumber-spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java Lines 52 to 58 in 51cde87
As you can see we create a test instance by pulling it from the application context. This ensures the test instance is a bean that can be injected into other step definition class instances and others can be injected into it. JUnit doesn't have this requirement (only one test class ever). So JUnit creates the test instance using the zero-arg constructor and outside of the application context. By invoking I have no idea what would happen if we do call |
This may provide a solution with |
Hello @mpkorstanje and thank you for your help. Unfortunately I don't really understand how to properly configure the AutowireCapableBeanFactory for cucumber context :( - do you have any examples of how it is done ? I tried searching on the internet but results are sparse. |
If you want to, you'd have to fix it in the |
I will try to do that tonight after work hours - being busy at the moment. |
Cheers. No rush though. |
@mpkorstanje #2655 opened. I mentioned you in the second commit message since you solved the issue :) |
To make writing tests with Spring easier Spring provides a `TestContextManager`. This classes provides call backs for various `TestExecutionListeners`. These are then used by various extensions such as the `MockitoTestExecutionListener` which injects `@MockBeans` into test instances. When all methods are not invoked this leads to problems such as (#2654,#2655,#2656) While this was initially (#1470) not a problem, it appears that various listener implementations have started to assume that all methods would be invoked. Closes: #2655 Fixes: #2654, #2572
To make writing tests with Spring easier Spring provides a `TestContextManager`. This classes provides call backs for various `TestExecutionListeners`. These are then used by various extensions such as the `MockitoTestExecutionListener` which injects `@MockBeans` into test instances. When all methods are not invoked this leads to problems such as (#2654,#2655,#2656) While this was initially (#1470) not a problem, it appears that various listener implementations have started to assume that all methods would be invoked. Closes: #2655 Fixes: #2654, #2572
To make writing tests with Spring easier Spring provides a `TestContextManager`. This classes provides call backs for various `TestExecutionListeners`. These are then used by various extensions such as the `MockitoTestExecutionListener` which injects `@MockBeans` into test instances. When all methods are not invoked this leads to problems such as (#2654,#2655,#2656) While this was initially (#1470) not a problem, it appears that various listener implementations have started to assume that all methods would be invoked. Closes: #2655 Fixes: #2654, #2572
To make writing tests with Spring easier Spring provides a `TestContextManager`. This classes provides call backs for various `TestExecutionListeners`. These are then used by various extensions such as the `MockitoTestExecutionListener` which injects `@MockBeans` into test instances. When all methods are not invoked this leads to problems such as (#2654,#2655,#2656) While this was initially (#1470) not a problem, it appears that various listener implementations have started to assume that all methods would be invoked. Closes: #2655 Fixes: #2654, #2572
To make writing tests with Spring easier Spring provides a `TestContextManager`. This classes provides call backs for various `TestExecutionListeners`. These are then used by various extensions such as the `MockitoTestExecutionListener` which injects `@MockBeans` into test instances. When all methods are not invoked this leads to problems such as (#2654,#2655,#2656) While this was initially (#1470) not a problem, it appears that various listener implementations have started to assume that all methods would be invoked. Closes: #2655 Fixes: #2654, #2572
π What did you see?
When running a SpringBootTest with JUnit5 (Jupiter) all @MockBean annotations are not populated.
β What did you expect to see?
Running the same test without cucumber configuration works with no issue.
π¦ Which tool/library version are you using?
Java - JDK 17
SpringBoot 2.7
Junit Jupiter - 5.8.2
cucumber-java - 7.8.1
cucumber-spring - 7.8.1
cucumber-junit-platform-engine - 7.8.1
π¬ How could we reproduce it?
Cucumber Test as described in the documentation
StepsFile
This text was originally generated from a template, then edited by hand. You can modify the template here.
The text was updated successfully, but these errors were encountered: