I’m trying to write tests where I just want to make sure that some code executes without throwing exceptions. For example:
SomeService service = new SomeService();
service.doSomething();
I’d like to use AssertJ’s fluent assertions to check that no exception is thrown, something like:
// pseudo-code idea
assertThatNoException().isThrownBy(() -> service.doSomething());
Is there a recommended way in AssertJ to do this? Can I also chain it with other assertions for better fluency?
             
            
              
              
              
            
           
          
            
            
              In my tests, I often use assertThatCode when I just want to ensure that a method doesn’t throw anything:
SomeService service = new SomeService();
assertThatCode(() -> service.doSomething())
    .doesNotThrowAnyException();
It’s straightforward and part of AssertJ’s fluent API. I like it because it reads almost like plain English.
             
            
              
              
              
            
           
          
            
            
              Sometimes I want to assert no exception and check the return value or state change.
For example:
SomeService service = new SomeService();
assertThatCode(() -> {
        service.doSomething();
        // you can include additional assertions here
        assertThat(service.getStatus()).isEqualTo("DONE");
    })
    .doesNotThrowAnyException();
I use this pattern when I want a single, fluent block to validate behavior and exceptions at the same time.
             
            
              
              
              
            
           
          
            
            
              If you need to inspect the exception later or log it for debugging, I sometimes do:
Throwable thrown = catchThrowable(() -> service.doSomething());
assertThat(thrown).isNull();
This approach is handy when I want a bit more control over what happens if an exception does occur, rather than just failing immediately.