Test that void method didn't get called with EasyMock
Is this possible?
I tried with EasyMock.expectLastCall().times(0);
but EasyMock complains that times must be >=1
java unit-testing easymock
add a comment |
Is this possible?
I tried with EasyMock.expectLastCall().times(0);
but EasyMock complains that times must be >=1
java unit-testing easymock
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
add a comment |
Is this possible?
I tried with EasyMock.expectLastCall().times(0);
but EasyMock complains that times must be >=1
java unit-testing easymock
Is this possible?
I tried with EasyMock.expectLastCall().times(0);
but EasyMock complains that times must be >=1
java unit-testing easymock
java unit-testing easymock
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
add a comment |
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
add a comment |
6 Answers
6
active
oldest
votes
You could use .andThrow(new AssertionFailedError()).anyTimes();
- this is the same exception that Assert.fail()
throws, but is less verbose than making an 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
add a comment |
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();
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
add a comment |
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 NiceMock
s, where default values are returned when calling for not recorded methods.
So a solution can be not to use NiceMock
s.
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 specifytimes(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
add a comment |
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()
?
yes, I did it that way eventually, see my answer
– nkr1pt
Sep 15 '10 at 20:54
add a comment |
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.
add a comment |
I managed to come up with a solution:
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
Assert.assertFail();
return null;
}
});
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You could use .andThrow(new AssertionFailedError()).anyTimes();
- this is the same exception that Assert.fail()
throws, but is less verbose than making an 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
add a comment |
You could use .andThrow(new AssertionFailedError()).anyTimes();
- this is the same exception that Assert.fail()
throws, but is less verbose than making an 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
add a comment |
You could use .andThrow(new AssertionFailedError()).anyTimes();
- this is the same exception that Assert.fail()
throws, but is less verbose than making an 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
.
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
add a comment |
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
add a comment |
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();
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
add a comment |
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();
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
add a comment |
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();
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();
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
add a comment |
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
add a comment |
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 NiceMock
s, where default values are returned when calling for not recorded methods.
So a solution can be not to use NiceMock
s.
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 specifytimes(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
add a comment |
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 NiceMock
s, where default values are returned when calling for not recorded methods.
So a solution can be not to use NiceMock
s.
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 specifytimes(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
add a comment |
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 NiceMock
s, where default values are returned when calling for not recorded methods.
So a solution can be not to use NiceMock
s.
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 NiceMock
s, where default values are returned when calling for not recorded methods.
So a solution can be not to use NiceMock
s.
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 specifytimes(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
add a comment |
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 specifytimes(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
add a comment |
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()
?
yes, I did it that way eventually, see my answer
– nkr1pt
Sep 15 '10 at 20:54
add a comment |
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()
?
yes, I did it that way eventually, see my answer
– nkr1pt
Sep 15 '10 at 20:54
add a comment |
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()
?
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()
?
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Sep 16 '10 at 21:54
Henri TremblayHenri Tremblay
59642
59642
add a comment |
add a comment |
I managed to come up with a solution:
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
Assert.assertFail();
return null;
}
});
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
add a comment |
I managed to come up with a solution:
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
Assert.assertFail();
return null;
}
});
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
add a comment |
I managed to come up with a solution:
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
Assert.assertFail();
return null;
}
});
I managed to come up with a solution:
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
Assert.assertFail();
return null;
}
});
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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