question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MockChannel: Overridden methods do not declare exceptions

See original GitHub issue

Issue 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:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ledoyencommented, Mar 6, 2020

If you do not need it, just close 😄

0reactions
joeltobycommented, Mar 5, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found