Test that void method didn't get called with EasyMock












14















Is this possible?
I tried with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1










share|improve this question

























  • See stackoverflow.com/questions/859031/easymock-void-methods , not sure it's a dupe though.

    – philant
    Sep 14 '10 at 18:45






  • 5





    It isn't a duplicate.

    – hiergiltdiestfu
    Jul 22 '13 at 10:30
















14















Is this possible?
I tried with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1










share|improve this question

























  • See stackoverflow.com/questions/859031/easymock-void-methods , not sure it's a dupe though.

    – philant
    Sep 14 '10 at 18:45






  • 5





    It isn't a duplicate.

    – hiergiltdiestfu
    Jul 22 '13 at 10:30














14












14








14


2






Is this possible?
I tried with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1










share|improve this question
















Is this possible?
I tried with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1







java unit-testing easymock






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 29 '18 at 0:08









risingTide

55141136




55141136










asked Sep 14 '10 at 16:09









nkr1ptnkr1pt

3,51942852




3,51942852













  • See stackoverflow.com/questions/859031/easymock-void-methods , not sure it's a dupe though.

    – philant
    Sep 14 '10 at 18:45






  • 5





    It isn't a duplicate.

    – hiergiltdiestfu
    Jul 22 '13 at 10:30



















  • See stackoverflow.com/questions/859031/easymock-void-methods , not sure it's a dupe though.

    – philant
    Sep 14 '10 at 18:45






  • 5





    It isn't a duplicate.

    – hiergiltdiestfu
    Jul 22 '13 at 10:30

















See stackoverflow.com/questions/859031/easymock-void-methods , not sure it's a dupe though.

– philant
Sep 14 '10 at 18:45





See stackoverflow.com/questions/859031/easymock-void-methods , not sure it's a dupe though.

– philant
Sep 14 '10 at 18:45




5




5





It isn't a duplicate.

– hiergiltdiestfu
Jul 22 '13 at 10:30





It isn't a duplicate.

– hiergiltdiestfu
Jul 22 '13 at 10:30












6 Answers
6






active

oldest

votes


















19














You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is the same exception that Assert.fail() throws, but is less verbose than making an Answer.






share|improve this answer





















  • 2





    Maybe adding a good description on why the test failed further improves this solution (which is the best of all given here IMO).

    – qben
    Feb 12 '15 at 10:27



















10














with easymock 3.0, you need to add a .anyTimes() on the expectLastCall or the test will fail:



Expectation failure on verify: myMethod(): expected: 1, actual: 0`


based on nkr1pt example:



expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
Assert.assertFail();
return null;
}
}).anyTimes();





share|improve this answer



















  • 3





    I think: Assert.assertFail(); should just be: Assert.fail();

    – Matt
    May 27 '13 at 21:06








  • 1





    While this is a working answer, I like the one from David Wallace better since it's less verbose.

    – qben
    Feb 12 '15 at 10:25



















7














The fact that some method is not called is controlled by Mock or StrictMock. They will throw an exception, when that not recorded method is called. This problem occurs only when using NiceMocks, where default values are returned when calling for not recorded methods.



So a solution can be not to use NiceMocks.






share|improve this answer





















  • 2





    I strongly disagree with the conclusion. Maybe, in this case, nice mock is not the best choice. But if you want to make sure one method will not get called, in fact this is the only thing you want to test, it's better to emphasize this with a mocked method that fails with an appropriate message.

    – qben
    Feb 12 '15 at 10:24






  • 1





    It is a good argument. I wanted to point this one out, since I had stumbled upon why I cannot specify times(0). And only later I realized, that that kind of contradicts with the idea of nice mocking, and not nice mocking does not allow unrecorded methods to be executed. Throwing an assert exception as the answer seemed more like a workaround for me and that 0 method calls should be specified using not nice mocks. Maybe I am a bit assertive in the conclusion. I'll just correct it.

    – Vic
    Feb 12 '15 at 11:54





















1














Looks like a bug to me. The internal class Range does not allow to set a maximum less than 1.



Couldn't you mock that method, and just call Assert.fail() ?






share|improve this answer
























  • yes, I did it that way eventually, see my answer

    – nkr1pt
    Sep 15 '10 at 20:54



















1














If you expect your method not to be called then just don't record it. But I agree it won't work with a nice mock.






share|improve this answer































    0














    I managed to come up with a solution:



    expectLastCall().andAnswer(new IAnswer() {
    public Object answer() {
    Assert.assertFail();
    return null;
    }
    });





    share|improve this answer
























    • For me it doesn't work. It says "expected: 1, actual: 0".

      – Vic
      Aug 23 '12 at 14:44






    • 1





      Agreed that this doesn't work. The correct answer is from David Nguyen.

      – dhaag23
      Sep 2 '14 at 21:01











    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%2f3710717%2ftest-that-void-method-didnt-get-called-with-easymock%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    6 Answers
    6






    active

    oldest

    votes








    6 Answers
    6






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    19














    You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is the same exception that Assert.fail() throws, but is less verbose than making an Answer.






    share|improve this answer





















    • 2





      Maybe adding a good description on why the test failed further improves this solution (which is the best of all given here IMO).

      – qben
      Feb 12 '15 at 10:27
















    19














    You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is the same exception that Assert.fail() throws, but is less verbose than making an Answer.






    share|improve this answer





















    • 2





      Maybe adding a good description on why the test failed further improves this solution (which is the best of all given here IMO).

      – qben
      Feb 12 '15 at 10:27














    19












    19








    19







    You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is the same exception that Assert.fail() throws, but is less verbose than making an Answer.






    share|improve this answer















    You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is the same exception that Assert.fail() throws, but is less verbose than making an Answer.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 16 '13 at 21:34

























    answered Jan 16 '13 at 20:58









    Dawood ibn KareemDawood ibn Kareem

    56.4k106390




    56.4k106390








    • 2





      Maybe adding a good description on why the test failed further improves this solution (which is the best of all given here IMO).

      – qben
      Feb 12 '15 at 10:27














    • 2





      Maybe adding a good description on why the test failed further improves this solution (which is the best of all given here IMO).

      – qben
      Feb 12 '15 at 10:27








    2




    2





    Maybe adding a good description on why the test failed further improves this solution (which is the best of all given here IMO).

    – qben
    Feb 12 '15 at 10:27





    Maybe adding a good description on why the test failed further improves this solution (which is the best of all given here IMO).

    – qben
    Feb 12 '15 at 10:27













    10














    with easymock 3.0, you need to add a .anyTimes() on the expectLastCall or the test will fail:



    Expectation failure on verify: myMethod(): expected: 1, actual: 0`


    based on nkr1pt example:



    expectLastCall().andAnswer(new IAnswer() {
    public Object answer() {
    Assert.assertFail();
    return null;
    }
    }).anyTimes();





    share|improve this answer



















    • 3





      I think: Assert.assertFail(); should just be: Assert.fail();

      – Matt
      May 27 '13 at 21:06








    • 1





      While this is a working answer, I like the one from David Wallace better since it's less verbose.

      – qben
      Feb 12 '15 at 10:25
















    10














    with easymock 3.0, you need to add a .anyTimes() on the expectLastCall or the test will fail:



    Expectation failure on verify: myMethod(): expected: 1, actual: 0`


    based on nkr1pt example:



    expectLastCall().andAnswer(new IAnswer() {
    public Object answer() {
    Assert.assertFail();
    return null;
    }
    }).anyTimes();





    share|improve this answer



















    • 3





      I think: Assert.assertFail(); should just be: Assert.fail();

      – Matt
      May 27 '13 at 21:06








    • 1





      While this is a working answer, I like the one from David Wallace better since it's less verbose.

      – qben
      Feb 12 '15 at 10:25














    10












    10








    10







    with easymock 3.0, you need to add a .anyTimes() on the expectLastCall or the test will fail:



    Expectation failure on verify: myMethod(): expected: 1, actual: 0`


    based on nkr1pt example:



    expectLastCall().andAnswer(new IAnswer() {
    public Object answer() {
    Assert.assertFail();
    return null;
    }
    }).anyTimes();





    share|improve this answer













    with easymock 3.0, you need to add a .anyTimes() on the expectLastCall or the test will fail:



    Expectation failure on verify: myMethod(): expected: 1, actual: 0`


    based on nkr1pt example:



    expectLastCall().andAnswer(new IAnswer() {
    public Object answer() {
    Assert.assertFail();
    return null;
    }
    }).anyTimes();






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered May 30 '11 at 13:43









    David NguyenDavid Nguyen

    19618




    19618








    • 3





      I think: Assert.assertFail(); should just be: Assert.fail();

      – Matt
      May 27 '13 at 21:06








    • 1





      While this is a working answer, I like the one from David Wallace better since it's less verbose.

      – qben
      Feb 12 '15 at 10:25














    • 3





      I think: Assert.assertFail(); should just be: Assert.fail();

      – Matt
      May 27 '13 at 21:06








    • 1





      While this is a working answer, I like the one from David Wallace better since it's less verbose.

      – qben
      Feb 12 '15 at 10:25








    3




    3





    I think: Assert.assertFail(); should just be: Assert.fail();

    – Matt
    May 27 '13 at 21:06







    I think: Assert.assertFail(); should just be: Assert.fail();

    – Matt
    May 27 '13 at 21:06






    1




    1





    While this is a working answer, I like the one from David Wallace better since it's less verbose.

    – qben
    Feb 12 '15 at 10:25





    While this is a working answer, I like the one from David Wallace better since it's less verbose.

    – qben
    Feb 12 '15 at 10:25











    7














    The fact that some method is not called is controlled by Mock or StrictMock. They will throw an exception, when that not recorded method is called. This problem occurs only when using NiceMocks, where default values are returned when calling for not recorded methods.



    So a solution can be not to use NiceMocks.






    share|improve this answer





















    • 2





      I strongly disagree with the conclusion. Maybe, in this case, nice mock is not the best choice. But if you want to make sure one method will not get called, in fact this is the only thing you want to test, it's better to emphasize this with a mocked method that fails with an appropriate message.

      – qben
      Feb 12 '15 at 10:24






    • 1





      It is a good argument. I wanted to point this one out, since I had stumbled upon why I cannot specify times(0). And only later I realized, that that kind of contradicts with the idea of nice mocking, and not nice mocking does not allow unrecorded methods to be executed. Throwing an assert exception as the answer seemed more like a workaround for me and that 0 method calls should be specified using not nice mocks. Maybe I am a bit assertive in the conclusion. I'll just correct it.

      – Vic
      Feb 12 '15 at 11:54


















    7














    The fact that some method is not called is controlled by Mock or StrictMock. They will throw an exception, when that not recorded method is called. This problem occurs only when using NiceMocks, where default values are returned when calling for not recorded methods.



    So a solution can be not to use NiceMocks.






    share|improve this answer





















    • 2





      I strongly disagree with the conclusion. Maybe, in this case, nice mock is not the best choice. But if you want to make sure one method will not get called, in fact this is the only thing you want to test, it's better to emphasize this with a mocked method that fails with an appropriate message.

      – qben
      Feb 12 '15 at 10:24






    • 1





      It is a good argument. I wanted to point this one out, since I had stumbled upon why I cannot specify times(0). And only later I realized, that that kind of contradicts with the idea of nice mocking, and not nice mocking does not allow unrecorded methods to be executed. Throwing an assert exception as the answer seemed more like a workaround for me and that 0 method calls should be specified using not nice mocks. Maybe I am a bit assertive in the conclusion. I'll just correct it.

      – Vic
      Feb 12 '15 at 11:54
















    7












    7








    7







    The fact that some method is not called is controlled by Mock or StrictMock. They will throw an exception, when that not recorded method is called. This problem occurs only when using NiceMocks, where default values are returned when calling for not recorded methods.



    So a solution can be not to use NiceMocks.






    share|improve this answer















    The fact that some method is not called is controlled by Mock or StrictMock. They will throw an exception, when that not recorded method is called. This problem occurs only when using NiceMocks, where default values are returned when calling for not recorded methods.



    So a solution can be not to use NiceMocks.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 12 '15 at 11:55

























    answered Aug 23 '12 at 14:51









    VicVic

    1,31021532




    1,31021532








    • 2





      I strongly disagree with the conclusion. Maybe, in this case, nice mock is not the best choice. But if you want to make sure one method will not get called, in fact this is the only thing you want to test, it's better to emphasize this with a mocked method that fails with an appropriate message.

      – qben
      Feb 12 '15 at 10:24






    • 1





      It is a good argument. I wanted to point this one out, since I had stumbled upon why I cannot specify times(0). And only later I realized, that that kind of contradicts with the idea of nice mocking, and not nice mocking does not allow unrecorded methods to be executed. Throwing an assert exception as the answer seemed more like a workaround for me and that 0 method calls should be specified using not nice mocks. Maybe I am a bit assertive in the conclusion. I'll just correct it.

      – Vic
      Feb 12 '15 at 11:54
















    • 2





      I strongly disagree with the conclusion. Maybe, in this case, nice mock is not the best choice. But if you want to make sure one method will not get called, in fact this is the only thing you want to test, it's better to emphasize this with a mocked method that fails with an appropriate message.

      – qben
      Feb 12 '15 at 10:24






    • 1





      It is a good argument. I wanted to point this one out, since I had stumbled upon why I cannot specify times(0). And only later I realized, that that kind of contradicts with the idea of nice mocking, and not nice mocking does not allow unrecorded methods to be executed. Throwing an assert exception as the answer seemed more like a workaround for me and that 0 method calls should be specified using not nice mocks. Maybe I am a bit assertive in the conclusion. I'll just correct it.

      – Vic
      Feb 12 '15 at 11:54










    2




    2





    I strongly disagree with the conclusion. Maybe, in this case, nice mock is not the best choice. But if you want to make sure one method will not get called, in fact this is the only thing you want to test, it's better to emphasize this with a mocked method that fails with an appropriate message.

    – qben
    Feb 12 '15 at 10:24





    I strongly disagree with the conclusion. Maybe, in this case, nice mock is not the best choice. But if you want to make sure one method will not get called, in fact this is the only thing you want to test, it's better to emphasize this with a mocked method that fails with an appropriate message.

    – qben
    Feb 12 '15 at 10:24




    1




    1





    It is a good argument. I wanted to point this one out, since I had stumbled upon why I cannot specify times(0). And only later I realized, that that kind of contradicts with the idea of nice mocking, and not nice mocking does not allow unrecorded methods to be executed. Throwing an assert exception as the answer seemed more like a workaround for me and that 0 method calls should be specified using not nice mocks. Maybe I am a bit assertive in the conclusion. I'll just correct it.

    – Vic
    Feb 12 '15 at 11:54







    It is a good argument. I wanted to point this one out, since I had stumbled upon why I cannot specify times(0). And only later I realized, that that kind of contradicts with the idea of nice mocking, and not nice mocking does not allow unrecorded methods to be executed. Throwing an assert exception as the answer seemed more like a workaround for me and that 0 method calls should be specified using not nice mocks. Maybe I am a bit assertive in the conclusion. I'll just correct it.

    – Vic
    Feb 12 '15 at 11:54













    1














    Looks like a bug to me. The internal class Range does not allow to set a maximum less than 1.



    Couldn't you mock that method, and just call Assert.fail() ?






    share|improve this answer
























    • yes, I did it that way eventually, see my answer

      – nkr1pt
      Sep 15 '10 at 20:54
















    1














    Looks like a bug to me. The internal class Range does not allow to set a maximum less than 1.



    Couldn't you mock that method, and just call Assert.fail() ?






    share|improve this answer
























    • yes, I did it that way eventually, see my answer

      – nkr1pt
      Sep 15 '10 at 20:54














    1












    1








    1







    Looks like a bug to me. The internal class Range does not allow to set a maximum less than 1.



    Couldn't you mock that method, and just call Assert.fail() ?






    share|improve this answer













    Looks like a bug to me. The internal class Range does not allow to set a maximum less than 1.



    Couldn't you mock that method, and just call Assert.fail() ?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 15 '10 at 6:54









    Christian StrempferChristian Strempfer

    5,69443868




    5,69443868













    • yes, I did it that way eventually, see my answer

      – nkr1pt
      Sep 15 '10 at 20:54



















    • yes, I did it that way eventually, see my answer

      – nkr1pt
      Sep 15 '10 at 20:54

















    yes, I did it that way eventually, see my answer

    – nkr1pt
    Sep 15 '10 at 20:54





    yes, I did it that way eventually, see my answer

    – nkr1pt
    Sep 15 '10 at 20:54











    1














    If you expect your method not to be called then just don't record it. But I agree it won't work with a nice mock.






    share|improve this answer




























      1














      If you expect your method not to be called then just don't record it. But I agree it won't work with a nice mock.






      share|improve this answer


























        1












        1








        1







        If you expect your method not to be called then just don't record it. But I agree it won't work with a nice mock.






        share|improve this answer













        If you expect your method not to be called then just don't record it. But I agree it won't work with a nice mock.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 16 '10 at 21:54









        Henri TremblayHenri Tremblay

        59642




        59642























            0














            I managed to come up with a solution:



            expectLastCall().andAnswer(new IAnswer() {
            public Object answer() {
            Assert.assertFail();
            return null;
            }
            });





            share|improve this answer
























            • For me it doesn't work. It says "expected: 1, actual: 0".

              – Vic
              Aug 23 '12 at 14:44






            • 1





              Agreed that this doesn't work. The correct answer is from David Nguyen.

              – dhaag23
              Sep 2 '14 at 21:01
















            0














            I managed to come up with a solution:



            expectLastCall().andAnswer(new IAnswer() {
            public Object answer() {
            Assert.assertFail();
            return null;
            }
            });





            share|improve this answer
























            • For me it doesn't work. It says "expected: 1, actual: 0".

              – Vic
              Aug 23 '12 at 14:44






            • 1





              Agreed that this doesn't work. The correct answer is from David Nguyen.

              – dhaag23
              Sep 2 '14 at 21:01














            0












            0








            0







            I managed to come up with a solution:



            expectLastCall().andAnswer(new IAnswer() {
            public Object answer() {
            Assert.assertFail();
            return null;
            }
            });





            share|improve this answer













            I managed to come up with a solution:



            expectLastCall().andAnswer(new IAnswer() {
            public Object answer() {
            Assert.assertFail();
            return null;
            }
            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Sep 15 '10 at 20:54









            nkr1ptnkr1pt

            3,51942852




            3,51942852













            • For me it doesn't work. It says "expected: 1, actual: 0".

              – Vic
              Aug 23 '12 at 14:44






            • 1





              Agreed that this doesn't work. The correct answer is from David Nguyen.

              – dhaag23
              Sep 2 '14 at 21:01



















            • For me it doesn't work. It says "expected: 1, actual: 0".

              – Vic
              Aug 23 '12 at 14:44






            • 1





              Agreed that this doesn't work. The correct answer is from David Nguyen.

              – dhaag23
              Sep 2 '14 at 21:01

















            For me it doesn't work. It says "expected: 1, actual: 0".

            – Vic
            Aug 23 '12 at 14:44





            For me it doesn't work. It says "expected: 1, actual: 0".

            – Vic
            Aug 23 '12 at 14:44




            1




            1





            Agreed that this doesn't work. The correct answer is from David Nguyen.

            – dhaag23
            Sep 2 '14 at 21:01





            Agreed that this doesn't work. The correct answer is from David Nguyen.

            – dhaag23
            Sep 2 '14 at 21:01


















            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%2f3710717%2ftest-that-void-method-didnt-get-called-with-easymock%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

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues