MockChannel: Overridden methods do not declare exceptions
See original GitHub issueIssue summary:
Methods in MockChannel which override Channel interface methods do not declare the same exceptions. For example, basicAck
does not declare an IOException
:
Channel:
void basicAck(long deliveryTag, boolean multiple) throws IOException;
MockChannel:
@Override
public void basicAck(long deliveryTag, boolean multiple) {
getTransactionOrNode().basicAck(deliveryTag, multiple);
metricsCollectorWrapper.basicAck(this, deliveryTag, multiple);
}
Why it matters:
Without the declared exceptions, it is not possible to mock throwing of the checked exception. Example / use-case:
try(Channel channel = Mockito.spy(conn.createChannel())) {
assertThat(channel).isInstanceOf(MockChannel.class);
Mockito.doThrow(new IOException("ACK failed"))
.when(channel)
.basicAck(anyLong(), anyBoolean());
// If the ACK fails (i.e. throws an IOException, my app will try 3 times.
// This is the behaviour I want to test
verify(channel, times(3)).basicAck(anyLong(), anyBoolean());
}
Due to the undeclared exception, this test results in an error: Checked exception is invalid for this method!
Is this a valid use case?
Is this a valid use-case, or am I using rabbitmq-mock incorrectly?
If its a valid issue, I’m happy to create a PR to fix it if that’s OK?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Overridden methods cannot throw exceptions Java [duplicate]
This answer is completely correct: All methods may throw unchecked exceptions, whether declared or not. This means that the overridden method can throw...
Read more >Exception Handling with Method Overriding in Java
If SuperClass does not declare an exception, then the SubClass can only declare unchecked exceptions, but not the checked exceptions. If ...
Read more >Exception Handling with Method Overriding in Java - Javatpoint
Rule 1: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception. Let's consider following ...
Read more >Method overriding and exceptions - Oracle Communities
The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method.
Read more >Exception Handling with Method Overriding in Java
Learn about Method Overriding and how to handle exceptions that occur in it. ... If the superclass method does not declare an exception, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you do not need it, just close 😄
@ledoyen - that sounds reasonable. I’m happy to update the sigs if that helps (and then close the issue)? If you’d prefer to leave it for now, I’ll just close.