{"url":"https://api.github.com/gists/700cb3bfd961e1566682040fce6d4e98","forks_url":"https://api.github.com/gists/700cb3bfd961e1566682040fce6d4e98/forks","commits_url":"https://api.github.com/gists/700cb3bfd961e1566682040fce6d4e98/commits","id":"700cb3bfd961e1566682040fce6d4e98","node_id":"MDQ6R2lzdDcwMGNiM2JmZDk2MWUxNTY2NjgyMDQwZmNlNmQ0ZTk4","git_pull_url":"https://gist.github.com/700cb3bfd961e1566682040fce6d4e98.git","git_push_url":"https://gist.github.com/700cb3bfd961e1566682040fce6d4e98.git","html_url":"https://gist.github.com/michaellihs/700cb3bfd961e1566682040fce6d4e98","files":{"spring-bean-injection.md":{"filename":"spring-bean-injection.md","type":"text/markdown","language":"Markdown","raw_url":"https://gist.githubusercontent.com/michaellihs/700cb3bfd961e1566682040fce6d4e98/raw/ec468e617952cd16919bc4108dd99dc688a4babb/spring-bean-injection.md","size":2358,"truncated":false,"content":"Given [this question on Stackoverflow](http://stackoverflow.com/questions/39778265/what-is-the-preferable-way-for-passing-bean-dependencies-in-spring) I wanted to check whether there is a difference between this style of Bean definition (variant 1):\n\n```java\n@Configuration\npublic class BeanConfiguration {\n\n    @Bean\n    public FirstClass firstClass() {\n        return new FirstClass();\n    }\n\n    @Bean\n    public SecondClass secondClass() {\n        return new SecondClass(firstClass());\n    }\n\n    public static class FirstClass {\n\n        public String get() {\n            return \"FirstClass\";\n        }\n\n    }\n\n    public class SecondClass {\n\n        private final FirstClass firstClass;\n\n        public SecondClass(FirstClass firstClass) {\n            this.firstClass = firstClass;\n        }\n\n        public String get() {\n            return firstClass.get();\n        }\n\n    }\n\n}\n```\n\nand this style (variant 2):\n\n```java\n@Configuration\npublic class BeanConfiguration {\n\n    @Bean\n    public FirstClass firstClass() {\n        return new FirstClass();\n    }\n\n    @Bean\n    public SecondClass secondClass(FirstClass firstClass) {\n        return new SecondClass(firstClass);\n    }\n\n    public static class FirstClass {\n\n        public String get() {\n            return \"FirstClass\";\n        }\n\n    }\n\n    public class SecondClass {\n\n        private final FirstClass firstClass;\n\n        public SecondClass(FirstClass firstClass) {\n            this.firstClass = firstClass;\n        }\n\n        public String get() {\n            return firstClass.get();\n        }\n\n    }\n\n}\n```\n\nGiven the [documentation for mocking beans in the Spring Docs](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-mocking-beans) one could assume that mocking does not properly work with variant 1, but the following test works with both variants:\n\n```java\n@RunWith(SpringRunner.class)\n@SpringBootTest(classes = BeanConfiguration.class)\npublic class BeanInjectionTest {\n\n    @MockBean\n    private BeanConfiguration.FirstClass firstClass;\n\n    @Autowired\n    private BeanConfiguration.SecondClass secondClass;\n\n    @Test\n    public void testInjectionWithMockedDependency() {\n        given(this.firstClass.get()).willReturn(\"mock\");\n        assertThat(secondClass.get(), is(\"mock\"));\n    }\n\n}\n```","encoding":"utf-8"}},"public":true,"created_at":"2016-09-29T20:08:45Z","updated_at":"2016-09-29T20:09:08Z","description":"Spring Bean Injection","comments":0,"user":null,"comments_enabled":true,"comments_url":"https://api.github.com/gists/700cb3bfd961e1566682040fce6d4e98/comments","owner":{"login":"michaellihs","id":575011,"node_id":"MDQ6VXNlcjU3NTAxMQ==","avatar_url":"https://avatars.githubusercontent.com/u/575011?v=4","gravatar_id":"","url":"https://api.github.com/users/michaellihs","html_url":"https://github.com/michaellihs","followers_url":"https://api.github.com/users/michaellihs/followers","following_url":"https://api.github.com/users/michaellihs/following{/other_user}","gists_url":"https://api.github.com/users/michaellihs/gists{/gist_id}","starred_url":"https://api.github.com/users/michaellihs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/michaellihs/subscriptions","organizations_url":"https://api.github.com/users/michaellihs/orgs","repos_url":"https://api.github.com/users/michaellihs/repos","events_url":"https://api.github.com/users/michaellihs/events{/privacy}","received_events_url":"https://api.github.com/users/michaellihs/received_events","type":"User","user_view_type":"public","site_admin":false},"forks":[],"history":[{"user":{"login":"michaellihs","id":575011,"node_id":"MDQ6VXNlcjU3NTAxMQ==","avatar_url":"https://avatars.githubusercontent.com/u/575011?v=4","gravatar_id":"","url":"https://api.github.com/users/michaellihs","html_url":"https://github.com/michaellihs","followers_url":"https://api.github.com/users/michaellihs/followers","following_url":"https://api.github.com/users/michaellihs/following{/other_user}","gists_url":"https://api.github.com/users/michaellihs/gists{/gist_id}","starred_url":"https://api.github.com/users/michaellihs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/michaellihs/subscriptions","organizations_url":"https://api.github.com/users/michaellihs/orgs","repos_url":"https://api.github.com/users/michaellihs/repos","events_url":"https://api.github.com/users/michaellihs/events{/privacy}","received_events_url":"https://api.github.com/users/michaellihs/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"6740baeaf84e43679510049b252c78d8c25af10a","committed_at":"2016-09-29T20:09:08Z","change_status":{"total":2,"additions":1,"deletions":1},"url":"https://api.github.com/gists/700cb3bfd961e1566682040fce6d4e98/6740baeaf84e43679510049b252c78d8c25af10a"},{"user":{"login":"michaellihs","id":575011,"node_id":"MDQ6VXNlcjU3NTAxMQ==","avatar_url":"https://avatars.githubusercontent.com/u/575011?v=4","gravatar_id":"","url":"https://api.github.com/users/michaellihs","html_url":"https://github.com/michaellihs","followers_url":"https://api.github.com/users/michaellihs/followers","following_url":"https://api.github.com/users/michaellihs/following{/other_user}","gists_url":"https://api.github.com/users/michaellihs/gists{/gist_id}","starred_url":"https://api.github.com/users/michaellihs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/michaellihs/subscriptions","organizations_url":"https://api.github.com/users/michaellihs/orgs","repos_url":"https://api.github.com/users/michaellihs/repos","events_url":"https://api.github.com/users/michaellihs/events{/privacy}","received_events_url":"https://api.github.com/users/michaellihs/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"6f4b6cf01f08c2158d975ef3678a09441ab7bc04","committed_at":"2016-09-29T20:08:45Z","change_status":{"total":103,"additions":103,"deletions":0},"url":"https://api.github.com/gists/700cb3bfd961e1566682040fce6d4e98/6f4b6cf01f08c2158d975ef3678a09441ab7bc04"}],"truncated":false}