{"id":37435602,"url":"https://github.com/antoinemeyer/mock-in-bean","last_synced_at":"2026-01-16T06:39:46.616Z","repository":{"id":43197232,"uuid":"348512450","full_name":"antoinemeyer/mock-in-bean","owner":"antoinemeyer","description":"Surgically Inject Mockito Mock/Spy in Spring Beans","archived":false,"fork":false,"pushed_at":"2025-01-09T21:44:31.000Z","size":112,"stargazers_count":95,"open_issues_count":3,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-30T03:41:13.932Z","etag":null,"topics":["junit","mockito","spring-boot"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antoinemeyer.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-16T22:42:25.000Z","updated_at":"2025-07-24T14:36:30.000Z","dependencies_parsed_at":"2024-03-23T16:29:24.281Z","dependency_job_id":"785aaa8d-b16c-43df-8312-07ad20732566","html_url":"https://github.com/antoinemeyer/mock-in-bean","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/antoinemeyer/mock-in-bean","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoinemeyer%2Fmock-in-bean","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoinemeyer%2Fmock-in-bean/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoinemeyer%2Fmock-in-bean/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoinemeyer%2Fmock-in-bean/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antoinemeyer","download_url":"https://codeload.github.com/antoinemeyer/mock-in-bean/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antoinemeyer%2Fmock-in-bean/sbom","scorecard":{"id":199334,"data":{"date":"2025-08-11","repo":{"name":"github.com/antoinemeyer/mock-in-bean","commit":"8e3d6a5c37355c3b7e3e703b277f13af856bd306"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.5,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":1,"reason":"Found 3/25 approved changesets -- score normalized to 1","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/maven.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/maven.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/antoinemeyer/mock-in-bean/maven.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/maven.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/antoinemeyer/mock-in-bean/maven.yml/master?enable=pin","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE.txt:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE.txt:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 19 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":6,"reason":"4 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-rc42-6c7j-7h5r","Warn: Project is vulnerable to: GHSA-4gc7-5j7h-4qph","Warn: Project is vulnerable to: GHSA-4wp7-92pw-q264","Warn: Project is vulnerable to: GHSA-9cmq-m9j5-mvww"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-16T22:32:22.748Z","repository_id":43197232,"created_at":"2025-08-16T22:32:22.749Z","updated_at":"2025-08-16T22:32:22.749Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28477913,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T06:30:42.265Z","status":"ssl_error","status_checked_at":"2026-01-16T06:30:16.248Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["junit","mockito","spring-boot"],"created_at":"2026-01-16T06:39:46.178Z","updated_at":"2026-01-16T06:39:46.609Z","avatar_url":"https://github.com/antoinemeyer.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mock in Bean\n\n[@MockInBean](src/main/java/com/teketik/test/mockinbean/MockInBean.java) and [@SpyInBean](src/main/java/com/teketik/test/mockinbean/SpyInBean.java) are alternatives to @MockBean and @SpyBean for Spring Boot tests *(\u003e= 2.6.15 including \u003e= 3.X.X)*.\n\nThey surgically replace a field value in a Spring Bean by a Mock/Spy for the duration of a test and set back the original value afterwards, leaving the Spring Context clean.\n\n**@MockInBean 'mocks a bean in a bean'** whereas **@MockBean 'mocks a bean in the whole context'**.\n\n# But why ?\n\n## The problem:\n\nSpring Context pollution was a fairly common problem before the introduction of @MockBean and @SpyBean. Many developers would inject mocks in beans during tests through `@InjectMock` or using manual setters and often forget to set back the original field values which would leave the Spring context polluted and cause test failures in unrelated tests.\n\n@MockBean and @SpyBean solved this issue by providing Mockito injection directly in the Spring Context but introduced an undesirable side-effect: their usage dirties the context and may lead to the re-creation of new Spring contexts for any unique combination, which can be incredibly time-consuming. See [The Problems with @MockBean](https://www.baeldung.com/spring-tests#2-the-problems-withmockbean)\n\n### The solution:\n\nAssuming you really need to run the test in the Spring Context, the most straight-forward solution is still to inject your mock/spy in your bean and reset it afterwards. \n\n**@MockInBean and @SpyInBean brings the convenience of @MockBean and @SpyBean and do just that:**\n  1. Set the mock/spy in the bean.\n  2. Replace the mock/spy by the original value afterwards.\n\n# Example:\n\nAssuming that you want to test the following service:\n```java\n@Service\npublic class MyService {\n\n    @Autowired\n    protected ThirdPartyApiService thirdPartyService;\n\n    @Autowired\n    protected ExpensiveProcessor expensiveProcessor;\n\n    public void doSomething() {\n        final Object somethingExpensive = expensiveProcessor.returnSomethingExpensive();\n        thirdPartyService.doSomethingOnThirdPartyApi(somethingExpensive);\n    }\n\n}\n\n```\n\nYou can write your test this way:\n```java\n@SpringBootTest\npublic class MyServiceTest {\n\n    @MockInBean(MyService.class)\n    private ThirdPartyApiService thirdPartyApiService;\n\n    @SpyInBean(MyService.class)\n    private ExpensiveProcessor expensiveProcessor;\n\n    @Autowired\n    private MyService myService;\n\n    @Test\n    public void test() {\n        final Object somethingExpensive = new Object();\n        Mockito.when(expensiveProcessor.returnSomethingExpensive()).thenReturn(somethingExpensive);\n        myService.doSomething();\n        Mockito.verify(thirdPartyApiService).doSomethingOnThirdPartyApi(somethingExpensive);\n    }\n\n}\n```\n\n**What happens**:\n\nBefore each test:\n  - `MyServiceTest.thirdPartyService` will be created as a mock and injected in the target of @MockInBean: `MyService`.\n  - `MyServiceTest.expensiveProcessor` will be created as a spy of the bean `expensiveProcessor` and injected in the target of @SpyInBean: `MyService`.\n  \nAfter the tests of `MyServiceTest` are done:\n  - `MyService.thirdPartyService` will be reset to the original Spring bean `thirdPartyService`\n  - `MyService.expensiveProcessor` will be reset to the original Spring bean `expensiveProcessor`\n\n# Usage:\n\nSimply include the maven dependency (from central maven) to start using @MockInBean and @SpyInBean in your tests.\n```\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.teketik\u003c/groupId\u003e\n  \u003cartifactId\u003emock-in-bean\u003c/artifactId\u003e\n  \u003cversion\u003eboot2-v1.9\u003c/version\u003e\n  \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\n@MockInBean and @SpyInBean also support:\n  - Injection in multiple Spring beans: *Repeat the annotation on your field*.\n  - Injection in bean identified by name if multiple instances exist in the context: *Specify a `name` in your annotation*.\n  \nCheckout the javadoc for more information.\n\n# Limitations:\n\nThis approach has some limitations compared to the @MockBean/@SpyBean equivalent:\n  - There is currently no isolation per thread. It is not advised to use this library in parallel test suites. (Parallel test execution is supported experimentally in https://github.com/antoinemeyer/mock-in-bean/tree/v2.1-RC).\n  - Operations on beans happening within another bean's constructor will not be performed against the mocks since the mocks are injected directly into the fields. Do not use @MockInBean/@SpyInBean for beans that are manipulated in constructors.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantoinemeyer%2Fmock-in-bean","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantoinemeyer%2Fmock-in-bean","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantoinemeyer%2Fmock-in-bean/lists"}