Is there a way to use @MockBean annotation while working with Spock frameworkTesting Spring Controller with @Autowired fields, returns null for fields but gets the controllerJMockit MockUp persisting between Spock testsSpring Boot 1.4 TestingSpring Test Service Autowiring resulting in nullSpock Mocking - my method call values aren't mocked@WebMvcTest giving 'Error creating bean with name' error for different service in spring boot testSpock mock returns null when mocking a class of spring stereotype “Repository” - Why?Mocking service spring class autowired by constructor param using spockMockito's MockBean annotation doesn't work in a Springboot rest controller unit testSpringBoot Container in Junit Test

Why are 150k or 200k jobs considered good when there are 300k+ births a month?

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

What defenses are there against being summoned by the Gate spell?

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

How is it possible to have an ability score that is less than 3?

Adding span tags within wp_list_pages list items

tikz: show 0 at the axis origin

Languages that we cannot (dis)prove to be Context-Free

Email Account under attack (really) - anything I can do?

Today is the Center

Can I make popcorn with any corn?

How to write a macro that is braces sensitive?

Arthur Somervell: 1000 Exercises - Meaning of this notation

What is the word for reserving something for yourself before others do?

Why not use SQL instead of GraphQL?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Theorems that impeded progress

Is it unprofessional to ask if a job posting on GlassDoor is real?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

Why Is Death Allowed In the Matrix?

How do I create uniquely male characters?

Why are electrically insulating heatsinks so rare? Is it just cost?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine



Is there a way to use @MockBean annotation while working with Spock framework


Testing Spring Controller with @Autowired fields, returns null for fields but gets the controllerJMockit MockUp persisting between Spock testsSpring Boot 1.4 TestingSpring Test Service Autowiring resulting in nullSpock Mocking - my method call values aren't mocked@WebMvcTest giving 'Error creating bean with name' error for different service in spring boot testSpock mock returns null when mocking a class of spring stereotype “Repository” - Why?Mocking service spring class autowired by constructor param using spockMockito's MockBean annotation doesn't work in a Springboot rest controller unit testSpringBoot Container in Junit Test






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I was trying out Spock and encountered an interesting problem when writing controller-tests.



WebMvcTest(value = SomeController.class)
@AutoConfigureMockMvc
@ActiveProfiles(value = "restapi")
@Import(value = SecurityConfiguration)
class AccountBalanceControllerTest extends Specification

@Autowired
SomeController someController

@MockBean
SomeService someService

def "lets test it"
given:
someService.findAllByName(_) >> ["Some", "Work"]
when:
def response = mockMvc.perform(get("/v1/someName/545465?fast=false").with(user("mvc-test").roles("SOME_ACCOUNTS")))
then:
response.andExpect(status().isOk())






So the problem is mocking method on SomeService instance does not work because it uses a different Mock class for mocking the instance of SomeService class. I got a work around using the static Mock method from Spock in the setup and then using a setter to set SomeService in the controller. My question is there any elegant way to use @MockBean with Spock Specification testing.










share|improve this question






























    0















    I was trying out Spock and encountered an interesting problem when writing controller-tests.



    WebMvcTest(value = SomeController.class)
    @AutoConfigureMockMvc
    @ActiveProfiles(value = "restapi")
    @Import(value = SecurityConfiguration)
    class AccountBalanceControllerTest extends Specification

    @Autowired
    SomeController someController

    @MockBean
    SomeService someService

    def "lets test it"
    given:
    someService.findAllByName(_) >> ["Some", "Work"]
    when:
    def response = mockMvc.perform(get("/v1/someName/545465?fast=false").with(user("mvc-test").roles("SOME_ACCOUNTS")))
    then:
    response.andExpect(status().isOk())






    So the problem is mocking method on SomeService instance does not work because it uses a different Mock class for mocking the instance of SomeService class. I got a work around using the static Mock method from Spock in the setup and then using a setter to set SomeService in the controller. My question is there any elegant way to use @MockBean with Spock Specification testing.










    share|improve this question


























      0












      0








      0


      1






      I was trying out Spock and encountered an interesting problem when writing controller-tests.



      WebMvcTest(value = SomeController.class)
      @AutoConfigureMockMvc
      @ActiveProfiles(value = "restapi")
      @Import(value = SecurityConfiguration)
      class AccountBalanceControllerTest extends Specification

      @Autowired
      SomeController someController

      @MockBean
      SomeService someService

      def "lets test it"
      given:
      someService.findAllByName(_) >> ["Some", "Work"]
      when:
      def response = mockMvc.perform(get("/v1/someName/545465?fast=false").with(user("mvc-test").roles("SOME_ACCOUNTS")))
      then:
      response.andExpect(status().isOk())






      So the problem is mocking method on SomeService instance does not work because it uses a different Mock class for mocking the instance of SomeService class. I got a work around using the static Mock method from Spock in the setup and then using a setter to set SomeService in the controller. My question is there any elegant way to use @MockBean with Spock Specification testing.










      share|improve this question
















      I was trying out Spock and encountered an interesting problem when writing controller-tests.



      WebMvcTest(value = SomeController.class)
      @AutoConfigureMockMvc
      @ActiveProfiles(value = "restapi")
      @Import(value = SecurityConfiguration)
      class AccountBalanceControllerTest extends Specification

      @Autowired
      SomeController someController

      @MockBean
      SomeService someService

      def "lets test it"
      given:
      someService.findAllByName(_) >> ["Some", "Work"]
      when:
      def response = mockMvc.perform(get("/v1/someName/545465?fast=false").with(user("mvc-test").roles("SOME_ACCOUNTS")))
      then:
      response.andExpect(status().isOk())






      So the problem is mocking method on SomeService instance does not work because it uses a different Mock class for mocking the instance of SomeService class. I got a work around using the static Mock method from Spock in the setup and then using a setter to set SomeService in the controller. My question is there any elegant way to use @MockBean with Spock Specification testing.







      spring-boot spock spring-test-mvc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 4:09









      Thomas Hirsch

      908622




      908622










      asked Mar 6 at 10:09









      pannupannu

      198113




      198113






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You should use @SpringBean instead of @MockBean. As its javadoc says:




          Inspired by Springs @MockBean, but adapted to Spock semantics







          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%2f55020459%2fis-there-a-way-to-use-mockbean-annotation-while-working-with-spock-framework%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You should use @SpringBean instead of @MockBean. As its javadoc says:




            Inspired by Springs @MockBean, but adapted to Spock semantics







            share|improve this answer



























              0














              You should use @SpringBean instead of @MockBean. As its javadoc says:




              Inspired by Springs @MockBean, but adapted to Spock semantics







              share|improve this answer

























                0












                0








                0







                You should use @SpringBean instead of @MockBean. As its javadoc says:




                Inspired by Springs @MockBean, but adapted to Spock semantics







                share|improve this answer













                You should use @SpringBean instead of @MockBean. As its javadoc says:




                Inspired by Springs @MockBean, but adapted to Spock semantics








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 12 at 16:55









                Dmitry KhamitovDmitry Khamitov

                76649




                76649





























                    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%2f55020459%2fis-there-a-way-to-use-mockbean-annotation-while-working-with-spock-framework%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

                    AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                    Алба-Юлія

                    Захаров Федір Захарович