Mockito - how to mock, and express expectations, on an abstract method of a class (i.e. a callback)
I'm trying to unit-test an abstract class, therefore need to provide a mock implementation to the class' abstract methods.
I think I'm in a niche situation (!) since neither @Mock
or @Spy
seem to help.
- tried
Mockito.mock(SimpleClient.class, Mockito.CALLS_REAL_METHODS)
but this totally shatters my abstract class (creates it without calling the constructor, running into NPE) - tried
@Spy
but this either requires an instance of the real class (which I cannot provide since it's abstract duh), or, it will attempt to call a no-arg constructor which doesn't exist.
(Context, although you should not need it: the class is a messaging client e.g. has sendMessage()
and abstract onMessage()
methods, and obviously I need to use the real implementation of sendMessage()
and mock the callback abstract onMessage()
)
java mocking mockito abstract spy
add a comment |
I'm trying to unit-test an abstract class, therefore need to provide a mock implementation to the class' abstract methods.
I think I'm in a niche situation (!) since neither @Mock
or @Spy
seem to help.
- tried
Mockito.mock(SimpleClient.class, Mockito.CALLS_REAL_METHODS)
but this totally shatters my abstract class (creates it without calling the constructor, running into NPE) - tried
@Spy
but this either requires an instance of the real class (which I cannot provide since it's abstract duh), or, it will attempt to call a no-arg constructor which doesn't exist.
(Context, although you should not need it: the class is a messaging client e.g. has sendMessage()
and abstract onMessage()
methods, and obviously I need to use the real implementation of sendMessage()
and mock the callback abstract onMessage()
)
java mocking mockito abstract spy
Is there a reason you cannot provide a dummy implementation instead? For example,sendMessage
simply puts your message on a list which the test can read?
– Joe C
Nov 25 '18 at 13:08
@JoeC yes, the reason is, I have in fact 12 callbacks and I feel ridiculous to provide dummies and continue to use Mockito despite not being able to help in such a basic, commonplace scenario
– haelix
Nov 25 '18 at 13:10
add a comment |
I'm trying to unit-test an abstract class, therefore need to provide a mock implementation to the class' abstract methods.
I think I'm in a niche situation (!) since neither @Mock
or @Spy
seem to help.
- tried
Mockito.mock(SimpleClient.class, Mockito.CALLS_REAL_METHODS)
but this totally shatters my abstract class (creates it without calling the constructor, running into NPE) - tried
@Spy
but this either requires an instance of the real class (which I cannot provide since it's abstract duh), or, it will attempt to call a no-arg constructor which doesn't exist.
(Context, although you should not need it: the class is a messaging client e.g. has sendMessage()
and abstract onMessage()
methods, and obviously I need to use the real implementation of sendMessage()
and mock the callback abstract onMessage()
)
java mocking mockito abstract spy
I'm trying to unit-test an abstract class, therefore need to provide a mock implementation to the class' abstract methods.
I think I'm in a niche situation (!) since neither @Mock
or @Spy
seem to help.
- tried
Mockito.mock(SimpleClient.class, Mockito.CALLS_REAL_METHODS)
but this totally shatters my abstract class (creates it without calling the constructor, running into NPE) - tried
@Spy
but this either requires an instance of the real class (which I cannot provide since it's abstract duh), or, it will attempt to call a no-arg constructor which doesn't exist.
(Context, although you should not need it: the class is a messaging client e.g. has sendMessage()
and abstract onMessage()
methods, and obviously I need to use the real implementation of sendMessage()
and mock the callback abstract onMessage()
)
java mocking mockito abstract spy
java mocking mockito abstract spy
asked Nov 25 '18 at 13:06
haelixhaelix
1,61832034
1,61832034
Is there a reason you cannot provide a dummy implementation instead? For example,sendMessage
simply puts your message on a list which the test can read?
– Joe C
Nov 25 '18 at 13:08
@JoeC yes, the reason is, I have in fact 12 callbacks and I feel ridiculous to provide dummies and continue to use Mockito despite not being able to help in such a basic, commonplace scenario
– haelix
Nov 25 '18 at 13:10
add a comment |
Is there a reason you cannot provide a dummy implementation instead? For example,sendMessage
simply puts your message on a list which the test can read?
– Joe C
Nov 25 '18 at 13:08
@JoeC yes, the reason is, I have in fact 12 callbacks and I feel ridiculous to provide dummies and continue to use Mockito despite not being able to help in such a basic, commonplace scenario
– haelix
Nov 25 '18 at 13:10
Is there a reason you cannot provide a dummy implementation instead? For example,
sendMessage
simply puts your message on a list which the test can read?– Joe C
Nov 25 '18 at 13:08
Is there a reason you cannot provide a dummy implementation instead? For example,
sendMessage
simply puts your message on a list which the test can read?– Joe C
Nov 25 '18 at 13:08
@JoeC yes, the reason is, I have in fact 12 callbacks and I feel ridiculous to provide dummies and continue to use Mockito despite not being able to help in such a basic, commonplace scenario
– haelix
Nov 25 '18 at 13:10
@JoeC yes, the reason is, I have in fact 12 callbacks and I feel ridiculous to provide dummies and continue to use Mockito despite not being able to help in such a basic, commonplace scenario
– haelix
Nov 25 '18 at 13:10
add a comment |
1 Answer
1
active
oldest
votes
I found the following
to go the first approach (
Mockito.mock
), they have added the ability to construct an underlying real object properly using an arbitrary constructor (And hopefully after that, Moquito goes on to "stub" each method as you express expectations on it, like the below):
MyClass mock = Mockito.mock(MyClass.class, Mockito.withSettings()
.useConstructor("stringArg", 17)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));
// ...
Mockito.when(mock.onMyAbstractMethod(Mockito.anyInt())).then(
invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)));
to go the second approach indeed a
@Spy
needs a created instance of the class, so in that case you really need to subclass it first.
Extra note: a separate issue, ugly as well, happens if say onMyAbstractMethod
returns void, in that case the Mockito.when...
syntax doesn't work due to a language limitation it seems, a fact which is poorly documented. One needs to do it the other way around and say:
Mockito.doAnswer(invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)))
.when(mock).onMyAbstractMethod(1);
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%2f53467745%2fmockito-how-to-mock-and-express-expectations-on-an-abstract-method-of-a-clas%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
I found the following
to go the first approach (
Mockito.mock
), they have added the ability to construct an underlying real object properly using an arbitrary constructor (And hopefully after that, Moquito goes on to "stub" each method as you express expectations on it, like the below):
MyClass mock = Mockito.mock(MyClass.class, Mockito.withSettings()
.useConstructor("stringArg", 17)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));
// ...
Mockito.when(mock.onMyAbstractMethod(Mockito.anyInt())).then(
invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)));
to go the second approach indeed a
@Spy
needs a created instance of the class, so in that case you really need to subclass it first.
Extra note: a separate issue, ugly as well, happens if say onMyAbstractMethod
returns void, in that case the Mockito.when...
syntax doesn't work due to a language limitation it seems, a fact which is poorly documented. One needs to do it the other way around and say:
Mockito.doAnswer(invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)))
.when(mock).onMyAbstractMethod(1);
add a comment |
I found the following
to go the first approach (
Mockito.mock
), they have added the ability to construct an underlying real object properly using an arbitrary constructor (And hopefully after that, Moquito goes on to "stub" each method as you express expectations on it, like the below):
MyClass mock = Mockito.mock(MyClass.class, Mockito.withSettings()
.useConstructor("stringArg", 17)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));
// ...
Mockito.when(mock.onMyAbstractMethod(Mockito.anyInt())).then(
invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)));
to go the second approach indeed a
@Spy
needs a created instance of the class, so in that case you really need to subclass it first.
Extra note: a separate issue, ugly as well, happens if say onMyAbstractMethod
returns void, in that case the Mockito.when...
syntax doesn't work due to a language limitation it seems, a fact which is poorly documented. One needs to do it the other way around and say:
Mockito.doAnswer(invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)))
.when(mock).onMyAbstractMethod(1);
add a comment |
I found the following
to go the first approach (
Mockito.mock
), they have added the ability to construct an underlying real object properly using an arbitrary constructor (And hopefully after that, Moquito goes on to "stub" each method as you express expectations on it, like the below):
MyClass mock = Mockito.mock(MyClass.class, Mockito.withSettings()
.useConstructor("stringArg", 17)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));
// ...
Mockito.when(mock.onMyAbstractMethod(Mockito.anyInt())).then(
invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)));
to go the second approach indeed a
@Spy
needs a created instance of the class, so in that case you really need to subclass it first.
Extra note: a separate issue, ugly as well, happens if say onMyAbstractMethod
returns void, in that case the Mockito.when...
syntax doesn't work due to a language limitation it seems, a fact which is poorly documented. One needs to do it the other way around and say:
Mockito.doAnswer(invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)))
.when(mock).onMyAbstractMethod(1);
I found the following
to go the first approach (
Mockito.mock
), they have added the ability to construct an underlying real object properly using an arbitrary constructor (And hopefully after that, Moquito goes on to "stub" each method as you express expectations on it, like the below):
MyClass mock = Mockito.mock(MyClass.class, Mockito.withSettings()
.useConstructor("stringArg", 17)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));
// ...
Mockito.when(mock.onMyAbstractMethod(Mockito.anyInt())).then(
invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)));
to go the second approach indeed a
@Spy
needs a created instance of the class, so in that case you really need to subclass it first.
Extra note: a separate issue, ugly as well, happens if say onMyAbstractMethod
returns void, in that case the Mockito.when...
syntax doesn't work due to a language limitation it seems, a fact which is poorly documented. One needs to do it the other way around and say:
Mockito.doAnswer(invocation -> mock.callAnotherRealMethodOnTheMock(invocation.getArgument(0)))
.when(mock).onMyAbstractMethod(1);
answered Nov 25 '18 at 22:31
haelixhaelix
1,61832034
1,61832034
add a comment |
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%2f53467745%2fmockito-how-to-mock-and-express-expectations-on-an-abstract-method-of-a-clas%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
Is there a reason you cannot provide a dummy implementation instead? For example,
sendMessage
simply puts your message on a list which the test can read?– Joe C
Nov 25 '18 at 13:08
@JoeC yes, the reason is, I have in fact 12 callbacks and I feel ridiculous to provide dummies and continue to use Mockito despite not being able to help in such a basic, commonplace scenario
– haelix
Nov 25 '18 at 13:10