Difference between @injectMocks and @Autowired usage in mockito?












8















When I was writing test case using the Mockito and Junit, I was using the @InjectMocks for the class to be tested. In other parts of project, I also see @Autowired is being used for the class to be tested.



When can I use @InjectMocks and @Autowired ? What is the difference between two when we are trying to use them with class to be tested ?










share|improve this question





























    8















    When I was writing test case using the Mockito and Junit, I was using the @InjectMocks for the class to be tested. In other parts of project, I also see @Autowired is being used for the class to be tested.



    When can I use @InjectMocks and @Autowired ? What is the difference between two when we are trying to use them with class to be tested ?










    share|improve this question



























      8












      8








      8


      3






      When I was writing test case using the Mockito and Junit, I was using the @InjectMocks for the class to be tested. In other parts of project, I also see @Autowired is being used for the class to be tested.



      When can I use @InjectMocks and @Autowired ? What is the difference between two when we are trying to use them with class to be tested ?










      share|improve this question
















      When I was writing test case using the Mockito and Junit, I was using the @InjectMocks for the class to be tested. In other parts of project, I also see @Autowired is being used for the class to be tested.



      When can I use @InjectMocks and @Autowired ? What is the difference between two when we are trying to use them with class to be tested ?







      java spring dependency-injection mockito spring-ioc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 26 '15 at 16:30









      luboskrnac

      15.4k54671




      15.4k54671










      asked Sep 17 '14 at 14:24









      Sai Srinadh KurraSai Srinadh Kurra

      4313




      4313
























          2 Answers
          2






          active

          oldest

          votes


















          7














          @InjectMocks is a Mockito mechanism for injecting declared fields in the test class into matching fields in the class under test. It doesn't require the class under test to be a Spring component.



          @Autowired is Spring's annotation for autowiring a bean into a production, non-test class.



          If you wanted to leverage the @Autowired annotations in the class under test, another approach would be to use springockito which allows you to declare mock beans so that they will be autowired into the class under test the same way that Spring would autowire the bean. But typically that's not necessary.






          share|improve this answer





















          • 1





            +1 as I had not heard of springockito which looks nice and clean.

            – dectarin
            Sep 17 '14 at 14:50











          • Springockito is really nice, I never use it as I favor unit tests. However I recently (a few month back) heard that springockito had some bugs.

            – Brice
            Sep 17 '14 at 16:41



















          9














          @InjectMocks annotation tells to Mockito to inject all mocks (objects annotated by @Mock annotation) into fields of testing object. Mockito uses Reflection for this.



          @Autowired annotation tells to Spring framework to inject bean from its IoC container. Spring also uses reflection for this when it is private field injection.
          You can even use even use @Inject annotation (part of Java EE specification) with the same effect.



          But I would suggest to look at benefits of Constructor injection over Field injection. In that case you don't need to use @InjectMocks at all, because you can pass mocks into testing object via constructor. There wouldn't be Reflection needed under the hood in your test nor in production.



          If you want to create integration test with subset of Spring beans I would suggest to take a look at @DirtiesContext annotation. It is part of Spring framework module commonly called "Spring Test".






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25893247%2fdifference-between-injectmocks-and-autowired-usage-in-mockito%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7














            @InjectMocks is a Mockito mechanism for injecting declared fields in the test class into matching fields in the class under test. It doesn't require the class under test to be a Spring component.



            @Autowired is Spring's annotation for autowiring a bean into a production, non-test class.



            If you wanted to leverage the @Autowired annotations in the class under test, another approach would be to use springockito which allows you to declare mock beans so that they will be autowired into the class under test the same way that Spring would autowire the bean. But typically that's not necessary.






            share|improve this answer





















            • 1





              +1 as I had not heard of springockito which looks nice and clean.

              – dectarin
              Sep 17 '14 at 14:50











            • Springockito is really nice, I never use it as I favor unit tests. However I recently (a few month back) heard that springockito had some bugs.

              – Brice
              Sep 17 '14 at 16:41
















            7














            @InjectMocks is a Mockito mechanism for injecting declared fields in the test class into matching fields in the class under test. It doesn't require the class under test to be a Spring component.



            @Autowired is Spring's annotation for autowiring a bean into a production, non-test class.



            If you wanted to leverage the @Autowired annotations in the class under test, another approach would be to use springockito which allows you to declare mock beans so that they will be autowired into the class under test the same way that Spring would autowire the bean. But typically that's not necessary.






            share|improve this answer





















            • 1





              +1 as I had not heard of springockito which looks nice and clean.

              – dectarin
              Sep 17 '14 at 14:50











            • Springockito is really nice, I never use it as I favor unit tests. However I recently (a few month back) heard that springockito had some bugs.

              – Brice
              Sep 17 '14 at 16:41














            7












            7








            7







            @InjectMocks is a Mockito mechanism for injecting declared fields in the test class into matching fields in the class under test. It doesn't require the class under test to be a Spring component.



            @Autowired is Spring's annotation for autowiring a bean into a production, non-test class.



            If you wanted to leverage the @Autowired annotations in the class under test, another approach would be to use springockito which allows you to declare mock beans so that they will be autowired into the class under test the same way that Spring would autowire the bean. But typically that's not necessary.






            share|improve this answer















            @InjectMocks is a Mockito mechanism for injecting declared fields in the test class into matching fields in the class under test. It doesn't require the class under test to be a Spring component.



            @Autowired is Spring's annotation for autowiring a bean into a production, non-test class.



            If you wanted to leverage the @Autowired annotations in the class under test, another approach would be to use springockito which allows you to declare mock beans so that they will be autowired into the class under test the same way that Spring would autowire the bean. But typically that's not necessary.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 17 '14 at 14:58

























            answered Sep 17 '14 at 14:27









            Mark PetersMark Peters

            66.9k11140175




            66.9k11140175








            • 1





              +1 as I had not heard of springockito which looks nice and clean.

              – dectarin
              Sep 17 '14 at 14:50











            • Springockito is really nice, I never use it as I favor unit tests. However I recently (a few month back) heard that springockito had some bugs.

              – Brice
              Sep 17 '14 at 16:41














            • 1





              +1 as I had not heard of springockito which looks nice and clean.

              – dectarin
              Sep 17 '14 at 14:50











            • Springockito is really nice, I never use it as I favor unit tests. However I recently (a few month back) heard that springockito had some bugs.

              – Brice
              Sep 17 '14 at 16:41








            1




            1





            +1 as I had not heard of springockito which looks nice and clean.

            – dectarin
            Sep 17 '14 at 14:50





            +1 as I had not heard of springockito which looks nice and clean.

            – dectarin
            Sep 17 '14 at 14:50













            Springockito is really nice, I never use it as I favor unit tests. However I recently (a few month back) heard that springockito had some bugs.

            – Brice
            Sep 17 '14 at 16:41





            Springockito is really nice, I never use it as I favor unit tests. However I recently (a few month back) heard that springockito had some bugs.

            – Brice
            Sep 17 '14 at 16:41













            9














            @InjectMocks annotation tells to Mockito to inject all mocks (objects annotated by @Mock annotation) into fields of testing object. Mockito uses Reflection for this.



            @Autowired annotation tells to Spring framework to inject bean from its IoC container. Spring also uses reflection for this when it is private field injection.
            You can even use even use @Inject annotation (part of Java EE specification) with the same effect.



            But I would suggest to look at benefits of Constructor injection over Field injection. In that case you don't need to use @InjectMocks at all, because you can pass mocks into testing object via constructor. There wouldn't be Reflection needed under the hood in your test nor in production.



            If you want to create integration test with subset of Spring beans I would suggest to take a look at @DirtiesContext annotation. It is part of Spring framework module commonly called "Spring Test".






            share|improve this answer






























              9














              @InjectMocks annotation tells to Mockito to inject all mocks (objects annotated by @Mock annotation) into fields of testing object. Mockito uses Reflection for this.



              @Autowired annotation tells to Spring framework to inject bean from its IoC container. Spring also uses reflection for this when it is private field injection.
              You can even use even use @Inject annotation (part of Java EE specification) with the same effect.



              But I would suggest to look at benefits of Constructor injection over Field injection. In that case you don't need to use @InjectMocks at all, because you can pass mocks into testing object via constructor. There wouldn't be Reflection needed under the hood in your test nor in production.



              If you want to create integration test with subset of Spring beans I would suggest to take a look at @DirtiesContext annotation. It is part of Spring framework module commonly called "Spring Test".






              share|improve this answer




























                9












                9








                9







                @InjectMocks annotation tells to Mockito to inject all mocks (objects annotated by @Mock annotation) into fields of testing object. Mockito uses Reflection for this.



                @Autowired annotation tells to Spring framework to inject bean from its IoC container. Spring also uses reflection for this when it is private field injection.
                You can even use even use @Inject annotation (part of Java EE specification) with the same effect.



                But I would suggest to look at benefits of Constructor injection over Field injection. In that case you don't need to use @InjectMocks at all, because you can pass mocks into testing object via constructor. There wouldn't be Reflection needed under the hood in your test nor in production.



                If you want to create integration test with subset of Spring beans I would suggest to take a look at @DirtiesContext annotation. It is part of Spring framework module commonly called "Spring Test".






                share|improve this answer















                @InjectMocks annotation tells to Mockito to inject all mocks (objects annotated by @Mock annotation) into fields of testing object. Mockito uses Reflection for this.



                @Autowired annotation tells to Spring framework to inject bean from its IoC container. Spring also uses reflection for this when it is private field injection.
                You can even use even use @Inject annotation (part of Java EE specification) with the same effect.



                But I would suggest to look at benefits of Constructor injection over Field injection. In that case you don't need to use @InjectMocks at all, because you can pass mocks into testing object via constructor. There wouldn't be Reflection needed under the hood in your test nor in production.



                If you want to create integration test with subset of Spring beans I would suggest to take a look at @DirtiesContext annotation. It is part of Spring framework module commonly called "Spring Test".







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 17 '14 at 15:21

























                answered Sep 17 '14 at 15:13









                luboskrnacluboskrnac

                15.4k54671




                15.4k54671






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25893247%2fdifference-between-injectmocks-and-autowired-usage-in-mockito%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to pass form data using jquery Ajax to insert data in database?

                    National Museum of Racing and Hall of Fame

                    Guess what letter conforming each word