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.

New Method Proposal: ExpectedException#expect(Throwable, Callable)

See original GitHub issue

Many times I needed to assert a block of code in test-method to throw an exception, and check how the throwing block has changed values. I had to use try-catch.

So I would like to implement new method with such of signature ExpectedException#expect(Throwable, Callable).

Usage:

Object obj = expect( Exception.class , new Callable() { ..do and return something.. });

Usage of Lambda in Java 8:

expect( Exception.class , () -> { ..do and return something.. } )

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:20 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
lukasedercommented, May 7, 2014

I was just wishing for the same! How about:

assertThrows(Exception.class, () -> foo());
assertThrows(Exception.class, () -> foo(), e -> assertEquals("yikes!", e.getMessage()));

Note that unlike @Tibor17, I’m suggesting a third argument that allows for adding additional checks on the Exception. I.e. we would have the following signatures:

@FunctionalInteface
interface ThrowableRunnable {
    void run() throws Throwable;
}

static void assertThrows(
    Class<? extends Throwable> throwable, 
    ThrowableRunnable runnable
) {}

static <T extends Throwable> void assertThrows(
    Class<Throwable> throwable, 
    ThrowableRunnable runnable, 
    Consumer<T> exceptionConsumer
) {}

Note also that neither Callable nor Runnable are really suited for this use-case. Neither of them allows for throwing Throwable

0reactions
kcooneycommented, Nov 30, 2016

Fixed in #1154

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you assert that a certain exception is thrown in JUnit ...
Be careful using expected exception, because it only asserts that the method threw that exception, not a particular line of code in the...
Read more >
Java 8 Friday: Better Exceptions - jOOQ blog
Every Friday, we're showing you a couple of nice new tutorial-style Java 8 features, ... ExpectedException#expect(Throwable, Callable) ...
Read more >
Callable (Java SE 16 & JDK 16) - Oracle Help Center
A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call . The...
Read more >
IllegalArgumentException.getCause - Java - Tabnine
Popular methods of IllegalArgumentException · <init>. Constructs a new exception with the specified cause and a detail message of (cause==null ? null :...
Read more >
Groovy Language Documentation
When a method (whether implemented in Java or Groovy) expects a java.lang. ... id case 1: return name } throw new IllegalArgumentException("No such...
Read more >

github_iconTop Related Medium Post

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