Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to verify that a method is wrapped with try-with-resources? #1389

Open
bfreitastgtg opened this issue Dec 19, 2024 · 0 comments
Open

How to verify that a method is wrapped with try-with-resources? #1389

bfreitastgtg opened this issue Dec 19, 2024 · 0 comments

Comments

@bfreitastgtg
Copy link

This is just a question. Is it possible to write an arch unit test that verifies that calls for certain methods are always wrapped with try-with-resources?

The method:

Stream<User> findAllByCustomQueryAndStream();

Compliant call:

try (Stream<User> stream = repository.findAllByCustomQueryAndStream()) {
  //do something with stream
}

Non-compliant call:

Stream<User> stream = repository.findAllByCustomQueryAndStream();
//do something with stream

Context: Spring repository methods that return Stream should be wrapped by a try-with-resources block, as suggested by the official docs.

I have tried inspecting the TryCatchBlock object but it does not give me information on what's actually being wrapped, it only contains info on the //do something with stream block and the caught exceptions.

@bfreitastgtg bfreitastgtg changed the title How to verify that a method is wrapped with try-with-resources How to verify that a method is wrapped with try-with-resources? Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant