How to doThrow or thenThrow on method that returns void and throws an exception
I have a method process
that returns void and also may throw an exception. I want to verify how does behave other method run
while calling process
and handles an exception if it occurs.
I've tried to use doThrow()
but it tells "Checked exception is invalid for this method!". Then I've tried to use thenThrow()
but it needs a not void function.
Code:
public void run() {
for (var billet : getBillets()) {
try {
process(billet);
billet.status = "processed";
} catch (Exception e) {
billet.status = "error";
}
billet.update();
}
}
public void process(Billet billet) throws Exception {
var data = parse(billet.data); // may throw an exception
var meta = data.get("meta"); // may throw an exception
// ... more parsing ...
new Product(meta).save();
new Item(meta).save();
// ... more operations ...
};
Test:
var billet1 = new Billet();
var billet2 = new Billet();
doThrow(new Exception()).when(myInctance).process(billet2);
myInctance.run();
assertEquals("processed", billet1.status);
assertEquals("error", billet2.status);
// ... some checks ...
I expect the test would succeeded.
java mockito
add a comment |
I have a method process
that returns void and also may throw an exception. I want to verify how does behave other method run
while calling process
and handles an exception if it occurs.
I've tried to use doThrow()
but it tells "Checked exception is invalid for this method!". Then I've tried to use thenThrow()
but it needs a not void function.
Code:
public void run() {
for (var billet : getBillets()) {
try {
process(billet);
billet.status = "processed";
} catch (Exception e) {
billet.status = "error";
}
billet.update();
}
}
public void process(Billet billet) throws Exception {
var data = parse(billet.data); // may throw an exception
var meta = data.get("meta"); // may throw an exception
// ... more parsing ...
new Product(meta).save();
new Item(meta).save();
// ... more operations ...
};
Test:
var billet1 = new Billet();
var billet2 = new Billet();
doThrow(new Exception()).when(myInctance).process(billet2);
myInctance.run();
assertEquals("processed", billet1.status);
assertEquals("error", billet2.status);
// ... some checks ...
I expect the test would succeeded.
java mockito
Possible duplicate of Mockito test a void method throws an exception
– Hulk
Jan 3 at 10:36
That sounds unlikely. My guess is that you oversimplified the code you're sharing, and that the code you really use is different. With the code you're showing there, you wouldn't have the results you're describing. You should show the code that you're really using instead of something else.
– kumesana
Jan 3 at 10:43
2
doThrow
does work for checked exceptions, but you need to throw the correct type of exception according to your method signature. If no checked exceptions are declared, throw aRuntimeException
instead ofException
.
– Hulk
Jan 3 at 10:44
@hulk, thanks. that worked!
– Aunmag
Jan 3 at 11:04
add a comment |
I have a method process
that returns void and also may throw an exception. I want to verify how does behave other method run
while calling process
and handles an exception if it occurs.
I've tried to use doThrow()
but it tells "Checked exception is invalid for this method!". Then I've tried to use thenThrow()
but it needs a not void function.
Code:
public void run() {
for (var billet : getBillets()) {
try {
process(billet);
billet.status = "processed";
} catch (Exception e) {
billet.status = "error";
}
billet.update();
}
}
public void process(Billet billet) throws Exception {
var data = parse(billet.data); // may throw an exception
var meta = data.get("meta"); // may throw an exception
// ... more parsing ...
new Product(meta).save();
new Item(meta).save();
// ... more operations ...
};
Test:
var billet1 = new Billet();
var billet2 = new Billet();
doThrow(new Exception()).when(myInctance).process(billet2);
myInctance.run();
assertEquals("processed", billet1.status);
assertEquals("error", billet2.status);
// ... some checks ...
I expect the test would succeeded.
java mockito
I have a method process
that returns void and also may throw an exception. I want to verify how does behave other method run
while calling process
and handles an exception if it occurs.
I've tried to use doThrow()
but it tells "Checked exception is invalid for this method!". Then I've tried to use thenThrow()
but it needs a not void function.
Code:
public void run() {
for (var billet : getBillets()) {
try {
process(billet);
billet.status = "processed";
} catch (Exception e) {
billet.status = "error";
}
billet.update();
}
}
public void process(Billet billet) throws Exception {
var data = parse(billet.data); // may throw an exception
var meta = data.get("meta"); // may throw an exception
// ... more parsing ...
new Product(meta).save();
new Item(meta).save();
// ... more operations ...
};
Test:
var billet1 = new Billet();
var billet2 = new Billet();
doThrow(new Exception()).when(myInctance).process(billet2);
myInctance.run();
assertEquals("processed", billet1.status);
assertEquals("error", billet2.status);
// ... some checks ...
I expect the test would succeeded.
java mockito
java mockito
asked Jan 3 at 10:27
AunmagAunmag
7816
7816
Possible duplicate of Mockito test a void method throws an exception
– Hulk
Jan 3 at 10:36
That sounds unlikely. My guess is that you oversimplified the code you're sharing, and that the code you really use is different. With the code you're showing there, you wouldn't have the results you're describing. You should show the code that you're really using instead of something else.
– kumesana
Jan 3 at 10:43
2
doThrow
does work for checked exceptions, but you need to throw the correct type of exception according to your method signature. If no checked exceptions are declared, throw aRuntimeException
instead ofException
.
– Hulk
Jan 3 at 10:44
@hulk, thanks. that worked!
– Aunmag
Jan 3 at 11:04
add a comment |
Possible duplicate of Mockito test a void method throws an exception
– Hulk
Jan 3 at 10:36
That sounds unlikely. My guess is that you oversimplified the code you're sharing, and that the code you really use is different. With the code you're showing there, you wouldn't have the results you're describing. You should show the code that you're really using instead of something else.
– kumesana
Jan 3 at 10:43
2
doThrow
does work for checked exceptions, but you need to throw the correct type of exception according to your method signature. If no checked exceptions are declared, throw aRuntimeException
instead ofException
.
– Hulk
Jan 3 at 10:44
@hulk, thanks. that worked!
– Aunmag
Jan 3 at 11:04
Possible duplicate of Mockito test a void method throws an exception
– Hulk
Jan 3 at 10:36
Possible duplicate of Mockito test a void method throws an exception
– Hulk
Jan 3 at 10:36
That sounds unlikely. My guess is that you oversimplified the code you're sharing, and that the code you really use is different. With the code you're showing there, you wouldn't have the results you're describing. You should show the code that you're really using instead of something else.
– kumesana
Jan 3 at 10:43
That sounds unlikely. My guess is that you oversimplified the code you're sharing, and that the code you really use is different. With the code you're showing there, you wouldn't have the results you're describing. You should show the code that you're really using instead of something else.
– kumesana
Jan 3 at 10:43
2
2
doThrow
does work for checked exceptions, but you need to throw the correct type of exception according to your method signature. If no checked exceptions are declared, throw a RuntimeException
instead of Exception
.– Hulk
Jan 3 at 10:44
doThrow
does work for checked exceptions, but you need to throw the correct type of exception according to your method signature. If no checked exceptions are declared, throw a RuntimeException
instead of Exception
.– Hulk
Jan 3 at 10:44
@hulk, thanks. that worked!
– Aunmag
Jan 3 at 11:04
@hulk, thanks. that worked!
– Aunmag
Jan 3 at 11:04
add a comment |
1 Answer
1
active
oldest
votes
This is the correct way to tell a mock to throw an exception:
Mockito.doThrow(new SomeException()).when(mock).doSomething()
As Hulk stated in the comments, this exception needs to match the method signature, otherwise, you will get a MockitoException("Checked exception is invalid for this method!")
You could get around that exception by throwing some type of RuntimeException
. Lastly, you should try to avoid using a generic Exception
. It is far more useful to throw an appropriate named exception.
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%2f54020425%2fhow-to-dothrow-or-thenthrow-on-method-that-returns-void-and-throws-an-exception%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
This is the correct way to tell a mock to throw an exception:
Mockito.doThrow(new SomeException()).when(mock).doSomething()
As Hulk stated in the comments, this exception needs to match the method signature, otherwise, you will get a MockitoException("Checked exception is invalid for this method!")
You could get around that exception by throwing some type of RuntimeException
. Lastly, you should try to avoid using a generic Exception
. It is far more useful to throw an appropriate named exception.
add a comment |
This is the correct way to tell a mock to throw an exception:
Mockito.doThrow(new SomeException()).when(mock).doSomething()
As Hulk stated in the comments, this exception needs to match the method signature, otherwise, you will get a MockitoException("Checked exception is invalid for this method!")
You could get around that exception by throwing some type of RuntimeException
. Lastly, you should try to avoid using a generic Exception
. It is far more useful to throw an appropriate named exception.
add a comment |
This is the correct way to tell a mock to throw an exception:
Mockito.doThrow(new SomeException()).when(mock).doSomething()
As Hulk stated in the comments, this exception needs to match the method signature, otherwise, you will get a MockitoException("Checked exception is invalid for this method!")
You could get around that exception by throwing some type of RuntimeException
. Lastly, you should try to avoid using a generic Exception
. It is far more useful to throw an appropriate named exception.
This is the correct way to tell a mock to throw an exception:
Mockito.doThrow(new SomeException()).when(mock).doSomething()
As Hulk stated in the comments, this exception needs to match the method signature, otherwise, you will get a MockitoException("Checked exception is invalid for this method!")
You could get around that exception by throwing some type of RuntimeException
. Lastly, you should try to avoid using a generic Exception
. It is far more useful to throw an appropriate named exception.
answered Jan 3 at 20:25
Phil NinanPhil Ninan
4881615
4881615
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%2f54020425%2fhow-to-dothrow-or-thenthrow-on-method-that-returns-void-and-throws-an-exception%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
Possible duplicate of Mockito test a void method throws an exception
– Hulk
Jan 3 at 10:36
That sounds unlikely. My guess is that you oversimplified the code you're sharing, and that the code you really use is different. With the code you're showing there, you wouldn't have the results you're describing. You should show the code that you're really using instead of something else.
– kumesana
Jan 3 at 10:43
2
doThrow
does work for checked exceptions, but you need to throw the correct type of exception according to your method signature. If no checked exceptions are declared, throw aRuntimeException
instead ofException
.– Hulk
Jan 3 at 10:44
@hulk, thanks. that worked!
– Aunmag
Jan 3 at 11:04