-
Notifications
You must be signed in to change notification settings - Fork 40.9k
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
Spies are not reset after test execution when using @SpyBean #33830
Comments
Thanks for the report and sample. I've reproduced the problem. The spy isn't being reset due to #7271 and the problem that we anticipated to some extent in this comment:
This is what's happening in your sample (although the I'm not sure how we can fix this without regressing 7271 so I'd like to discuss it with the rest of the team. In the meantime, your workaround is a good one. Alternatively, you may want to perform this sort of failure testing in a pure unit test that doesn't involve the application context. |
Thank you for your comment, I actually found a workaround so I can live on :-) Aside this bug, w.r.t. the testing strategy: I would indeed like to do unit testing instead of loading the entire application context, but I don't believe it's nice or wise to mock all methods of a JPA repo for the unit test. Does the spring/JPA philosophy like mocking so much that it would actually always mock this in the tests? I know there is some controversy about whether to use mocks always or not; but am I correct that there is no way to obtain an in-memory implementation of a JPA repo, so you can unittest with that dependency, and not having to mock the methods in the test? (i.e.. just observe the behavior from your unit under test)? |
We're going to try calling |
…ot-starter-parent from 2.7.8 to 2.7.9 (patch) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [org.springframework.boot:spring-boot-starter-parent](https://spring.io/projects/spring-boot) ([source](https://github.com/spring-projects/spring-boot)) | parent | patch | `2.7.8` -> `2.7.9` | --- ### Release Notes <details> <summary>spring-projects/spring-boot</summary> ### [`v2.7.9`](https://github.com/spring-projects/spring-boot/releases/tag/v2.7.9) [Compare Source](spring-projects/spring-boot@v2.7.8...v2.7.9) #### 🐞 Bug Fixes - Maven Plugin's PropertiesMergingResourceTransformer closes InputStream when it should not do so [#​34063](spring-projects/spring-boot#34063) - Actuator Health web endpoint broken with Gson and Java 17 [#​34030](spring-projects/spring-boot#34030) - Dependency management for Mongo's Java Driver is incomplete [#​33941](spring-projects/spring-boot#33941) - Using devtools with Reactive application results in slower restarts [#​33855](spring-projects/spring-boot#33855) - Spies are not reset after test execution when using `@SpyBean` [#​33830](spring-projects/spring-boot#33830) - Properties Migrator does not detect properties of Map type that are marked as deprecated [#​27854](spring-projects/spring-boot#27854) #### 📔 Documentation - Updated documentation for `@ConfigurationProperties` bean naming rules [#​34029](spring-projects/spring-boot#34029) - Restore "Use Jedis Instead of Lettuce" how-to documentation [#​33994](spring-projects/spring-boot#33994) - Add Redis application properties example [#​33965](spring-projects/spring-boot#33965) - Use Maven Central for release downloads in CLI installation documentation [#​33962](spring-projects/spring-boot#33962) - Actuator section is missing from documentation overview [#​33932](spring-projects/spring-boot#33932) - Add Javadoc since to OperationParameter.getAnnotation() [#​33914](spring-projects/spring-boot#33914) - Document additional configuration that is required for spring.mvc.throw-exception-if-no-handler-found=true to be effective [#​31660](spring-projects/spring-boot#31660) #### 🔨 Dependency Upgrades - Upgrade to ActiveMQ 5.16.6 [#​34238](spring-projects/spring-boot#34238) - Upgrade to Byte Buddy 1.12.23 [#​34239](spring-projects/spring-boot#34239) - Upgrade to Dropwizard Metrics 4.2.16 [#​34240](spring-projects/spring-boot#34240) - Upgrade to Elasticsearch 7.17.9 [#...
…ot-starter-parent from 2.7.8 to 2.7.9 (patch) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [org.springframework.boot:spring-boot-starter-parent](https://spring.io/projects/spring-boot) ([source](https://github.com/spring-projects/spring-boot)) | parent | patch | `2.7.8` -> `2.7.9` | --- ### Release Notes <details> <summary>spring-projects/spring-boot</summary> ### [`v2.7.9`](https://github.com/spring-projects/spring-boot/releases/tag/v2.7.9) [Compare Source](spring-projects/spring-boot@v2.7.8...v2.7.9) #### 🐞 Bug Fixes - Maven Plugin's PropertiesMergingResourceTransformer closes InputStream when it should not do so [#​34063](spring-projects/spring-boot#34063) - Actuator Health web endpoint broken with Gson and Java 17 [#​34030](spring-projects/spring-boot#34030) - Dependency management for Mongo's Java Driver is incomplete [#​33941](spring-projects/spring-boot#33941) - Using devtools with Reactive application results in slower restarts [#​33855](spring-projects/spring-boot#33855) - Spies are not reset after test execution when using `@SpyBean` [#​33830](spring-projects/spring-boot#33830) - Properties Migrator does not detect properties of Map type that are marked as deprecated [#​27854](spring-projects/spring-boot#27854) #### 📔 Documentation - Updated documentation for `@ConfigurationProperties` bean naming rules [#​34029](spring-projects/spring-boot#34029) - Restore "Use Jedis Instead of Lettuce" how-to documentation [#​33994](spring-projects/spring-boot#33994) - Add Redis application properties example [#​33965](spring-projects/spring-boot#33965) - Use Maven Central for release downloads in CLI installation documentation [#​33962](spring-projects/spring-boot#33962) - Actuator section is missing from documentation overview [#​33932](spring-projects/spring-boot#33932) - Add Javadoc since to OperationParameter.getAnnotation() [#​33914](spring-projects/spring-boot#33914) - Document additional configuration that is required for spring.mvc.throw-exception-if-no-handler-found=true to be effective [#​31660](spring-projects/spring-boot#31660) #### 🔨 Dependency Upgrades - Upgrade to ActiveMQ 5.16.6 [#​34238](spring-projects/spring-boot#34238) - Upgrade to Byte Buddy 1.12.23 [#​34239](spring-projects/spring-boot#34239) - Upgrade to Dropwizard Metrics 4.2.16 [#​34240](spring-projects/spring-boot#34240) - Upgrade to Elasticsearch 7.17.9 [#...
I experience the same problem as in #7374: The
@SpyBean
on the class is not reset between test cases.I create a small (minimal?) version to reproduce, as suggested in the mentioned issue.
See the git repo on https://github.com/feliksik/bug-reports/tree/master/spybean-spring.
For convenience, the test inline:
The first test mocks an exception (the test expects it, and succeeds), the second test suffers from this: it fails, as it does not expect an exception. (If I run them separately or change the alphabetic order of cases, all is fine).
The workaround I discovered is the reset() in the setup method.
Note that I could not reproduce this without the Jpa repo; but I want to simulate a database failure.
The text was updated successfully, but these errors were encountered: