spock, mock a method response in a spring bean












0















I have an integration test written in groovy (spock) in spring boot application. One of the application beans is called Validator it has the follwoing method:



public void validateIssueDates(final List<Timestamp> issueDates) {
issueDates.forEach(issueDate -> {
final Timestamp now = Timestamp.valueOf(LocalDateTime.now());

if (issueDate.before(now)) {
throw new IllegalArgumentException("Issue date is before current date");
}
});
}


In the Validator class there are other methods. In my spock integration test I would like to mock response for that particular method only. In the following way:



Validator.validateIssueDates(_) >> null


I want other validations to take place, but not this one. Bascially I want to achieve this but with spock. I would like to eliminate the validateIssueDates() method from being executed










share|improve this question




















  • 1





    You should put your solutions in an answer see stackoverflow.blog/2011/07/01/…

    – Leonard Brünings
    Nov 30 '18 at 17:10











  • good point. Done. Thanks for the remark.

    – user3529850
    Nov 30 '18 at 22:20
















0















I have an integration test written in groovy (spock) in spring boot application. One of the application beans is called Validator it has the follwoing method:



public void validateIssueDates(final List<Timestamp> issueDates) {
issueDates.forEach(issueDate -> {
final Timestamp now = Timestamp.valueOf(LocalDateTime.now());

if (issueDate.before(now)) {
throw new IllegalArgumentException("Issue date is before current date");
}
});
}


In the Validator class there are other methods. In my spock integration test I would like to mock response for that particular method only. In the following way:



Validator.validateIssueDates(_) >> null


I want other validations to take place, but not this one. Bascially I want to achieve this but with spock. I would like to eliminate the validateIssueDates() method from being executed










share|improve this question




















  • 1





    You should put your solutions in an answer see stackoverflow.blog/2011/07/01/…

    – Leonard Brünings
    Nov 30 '18 at 17:10











  • good point. Done. Thanks for the remark.

    – user3529850
    Nov 30 '18 at 22:20














0












0








0








I have an integration test written in groovy (spock) in spring boot application. One of the application beans is called Validator it has the follwoing method:



public void validateIssueDates(final List<Timestamp> issueDates) {
issueDates.forEach(issueDate -> {
final Timestamp now = Timestamp.valueOf(LocalDateTime.now());

if (issueDate.before(now)) {
throw new IllegalArgumentException("Issue date is before current date");
}
});
}


In the Validator class there are other methods. In my spock integration test I would like to mock response for that particular method only. In the following way:



Validator.validateIssueDates(_) >> null


I want other validations to take place, but not this one. Bascially I want to achieve this but with spock. I would like to eliminate the validateIssueDates() method from being executed










share|improve this question
















I have an integration test written in groovy (spock) in spring boot application. One of the application beans is called Validator it has the follwoing method:



public void validateIssueDates(final List<Timestamp> issueDates) {
issueDates.forEach(issueDate -> {
final Timestamp now = Timestamp.valueOf(LocalDateTime.now());

if (issueDate.before(now)) {
throw new IllegalArgumentException("Issue date is before current date");
}
});
}


In the Validator class there are other methods. In my spock integration test I would like to mock response for that particular method only. In the following way:



Validator.validateIssueDates(_) >> null


I want other validations to take place, but not this one. Bascially I want to achieve this but with spock. I would like to eliminate the validateIssueDates() method from being executed







spring spock






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 '18 at 22:17







user3529850

















asked Nov 28 '18 at 19:57









user3529850user3529850

476421




476421








  • 1





    You should put your solutions in an answer see stackoverflow.blog/2011/07/01/…

    – Leonard Brünings
    Nov 30 '18 at 17:10











  • good point. Done. Thanks for the remark.

    – user3529850
    Nov 30 '18 at 22:20














  • 1





    You should put your solutions in an answer see stackoverflow.blog/2011/07/01/…

    – Leonard Brünings
    Nov 30 '18 at 17:10











  • good point. Done. Thanks for the remark.

    – user3529850
    Nov 30 '18 at 22:20








1




1





You should put your solutions in an answer see stackoverflow.blog/2011/07/01/…

– Leonard Brünings
Nov 30 '18 at 17:10





You should put your solutions in an answer see stackoverflow.blog/2011/07/01/…

– Leonard Brünings
Nov 30 '18 at 17:10













good point. Done. Thanks for the remark.

– user3529850
Nov 30 '18 at 22:20





good point. Done. Thanks for the remark.

– user3529850
Nov 30 '18 at 22:20












1 Answer
1






active

oldest

votes


















0














solution using Spock



It's done using [@SpringSpy][2].

First we annotate field with a spring bean we want to wrap in spy object. For example:



@SpringSpy
private CarValidator carValidator;


then in our test, in then part we define how we want to override method from a a bean/spy:



then:
3 * carValidator.validateIssueDates(_) >> null




Solution using Mockito (as an additional approach, it's not related to spock solution)



I have got that pretty easy using spy in Mockito. Despite many trials (and errors) with spock's spy, It just doesn't want to work. If I get that, I post it here. For now, I can only share Mockito solution:



@Profile("test")
@Configuration
public class BeanConfig {

@Bean
@Primary
public CarValidator getCarValidatorSpy(CarValidator validator) {
CarValidator carValidatorSpy = Mockito.spy(validator);

Mockito.doNothing().when(carValidatorSpy).validateIssueDates(Mockito.any(CarDto.class));
return carValidatorSpy;
}
}




That's all. Seems fairly straightforward.






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%2f53527180%2fspock-mock-a-method-response-in-a-spring-bean%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














    solution using Spock



    It's done using [@SpringSpy][2].

    First we annotate field with a spring bean we want to wrap in spy object. For example:



    @SpringSpy
    private CarValidator carValidator;


    then in our test, in then part we define how we want to override method from a a bean/spy:



    then:
    3 * carValidator.validateIssueDates(_) >> null




    Solution using Mockito (as an additional approach, it's not related to spock solution)



    I have got that pretty easy using spy in Mockito. Despite many trials (and errors) with spock's spy, It just doesn't want to work. If I get that, I post it here. For now, I can only share Mockito solution:



    @Profile("test")
    @Configuration
    public class BeanConfig {

    @Bean
    @Primary
    public CarValidator getCarValidatorSpy(CarValidator validator) {
    CarValidator carValidatorSpy = Mockito.spy(validator);

    Mockito.doNothing().when(carValidatorSpy).validateIssueDates(Mockito.any(CarDto.class));
    return carValidatorSpy;
    }
    }




    That's all. Seems fairly straightforward.






    share|improve this answer




























      0














      solution using Spock



      It's done using [@SpringSpy][2].

      First we annotate field with a spring bean we want to wrap in spy object. For example:



      @SpringSpy
      private CarValidator carValidator;


      then in our test, in then part we define how we want to override method from a a bean/spy:



      then:
      3 * carValidator.validateIssueDates(_) >> null




      Solution using Mockito (as an additional approach, it's not related to spock solution)



      I have got that pretty easy using spy in Mockito. Despite many trials (and errors) with spock's spy, It just doesn't want to work. If I get that, I post it here. For now, I can only share Mockito solution:



      @Profile("test")
      @Configuration
      public class BeanConfig {

      @Bean
      @Primary
      public CarValidator getCarValidatorSpy(CarValidator validator) {
      CarValidator carValidatorSpy = Mockito.spy(validator);

      Mockito.doNothing().when(carValidatorSpy).validateIssueDates(Mockito.any(CarDto.class));
      return carValidatorSpy;
      }
      }




      That's all. Seems fairly straightforward.






      share|improve this answer


























        0












        0








        0







        solution using Spock



        It's done using [@SpringSpy][2].

        First we annotate field with a spring bean we want to wrap in spy object. For example:



        @SpringSpy
        private CarValidator carValidator;


        then in our test, in then part we define how we want to override method from a a bean/spy:



        then:
        3 * carValidator.validateIssueDates(_) >> null




        Solution using Mockito (as an additional approach, it's not related to spock solution)



        I have got that pretty easy using spy in Mockito. Despite many trials (and errors) with spock's spy, It just doesn't want to work. If I get that, I post it here. For now, I can only share Mockito solution:



        @Profile("test")
        @Configuration
        public class BeanConfig {

        @Bean
        @Primary
        public CarValidator getCarValidatorSpy(CarValidator validator) {
        CarValidator carValidatorSpy = Mockito.spy(validator);

        Mockito.doNothing().when(carValidatorSpy).validateIssueDates(Mockito.any(CarDto.class));
        return carValidatorSpy;
        }
        }




        That's all. Seems fairly straightforward.






        share|improve this answer













        solution using Spock



        It's done using [@SpringSpy][2].

        First we annotate field with a spring bean we want to wrap in spy object. For example:



        @SpringSpy
        private CarValidator carValidator;


        then in our test, in then part we define how we want to override method from a a bean/spy:



        then:
        3 * carValidator.validateIssueDates(_) >> null




        Solution using Mockito (as an additional approach, it's not related to spock solution)



        I have got that pretty easy using spy in Mockito. Despite many trials (and errors) with spock's spy, It just doesn't want to work. If I get that, I post it here. For now, I can only share Mockito solution:



        @Profile("test")
        @Configuration
        public class BeanConfig {

        @Bean
        @Primary
        public CarValidator getCarValidatorSpy(CarValidator validator) {
        CarValidator carValidatorSpy = Mockito.spy(validator);

        Mockito.doNothing().when(carValidatorSpy).validateIssueDates(Mockito.any(CarDto.class));
        return carValidatorSpy;
        }
        }




        That's all. Seems fairly straightforward.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 30 '18 at 22:20









        user3529850user3529850

        476421




        476421
































            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%2f53527180%2fspock-mock-a-method-response-in-a-spring-bean%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)