简体   繁体   中英

How to assert that execution path is not executed more than once in Java?

I am making an assumption that a code path is only executed once within the JVM. However, when writing complex, multi-threaded system sometimes the assumption can be wrong.

Are there any one liner assertion I can use to assert this. Something like Guava's Preconditions.checkState(expr) but more like assertAtMostTimes(1, "Should not be executing more than once") . Prefer something simple rather than tracking number of times in a class variable.

This is similar to how test cases can be written but I am looking for something in production code.

Check out the JaCoCo Code coverage tool. It has the ability to watch code paths and it keeps count of how many times they are executed. It isn't trivially easy to set up, but it should be able to do what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM