Enterprise testing
Exceptional cleanup test
Verify a fixture closes even on failure. Executed Java 21 fixture.
Formal Syntax & Structure
java syntaxjava
try (var resource = new Fixture(flag))Working Example
Example Snippetjava
public class Main { record Fixture(java.util.concurrent.atomic.AtomicBoolean closed) implements AutoCloseable { void fail(){throw new IllegalStateException("failed");} public void close(){closed.set(true);} } public static void main(String[] args) {var released=new java.util.concurrent.atomic.AtomicBoolean(); try(var resource=new Fixture(released)) {resource.fail();} catch(IllegalStateException e) {System.out.println(e.getMessage());} System.out.println(released.get()); }}