New Method Proposal: ExpectedException#expect(Throwable, Callable)
See original GitHub issueMany 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:
- Created 10 years ago
- Comments:20 (18 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
I was just wishing for the same! How about:
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:Note also that neither
Callable
norRunnable
are really suited for this use-case. Neither of them allows for throwingThrowable
Fixed in #1154