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.