sbabcoc / JUnit-Foundation

JUnit Foundation is a lightweight collection of JUnit watchers, interfaces, and static utility classes that supplement and augment the functionality provided by the JUnit API.
Apache License 2.0
22 stars 6 forks source link

Revise MutableTest class to use Advice instead of reflection #118

Closed sbabcoc closed 2 years ago

sbabcoc commented 2 years ago

Currently, the MutableTest class uses reflection to access the list of declared annotations of the Java Executable class. The objective is to replace the standard Test annotation with a mutable proxy to enable overriding the value of the timeout field. Reflective access to the required private field of the Executable class is no longer legal. This deprecated functionality can be permitted with the --illegal-access=permit option, but this is definitely sub-optimal. This illegal implementation should be replaced with supported functionality implemented with the Advice feature of Byte Buddy.

An example of this sort of implementation can be found here. A comprehensive guide can be found here. To intercept private methods and access private fields, check this. The MemberSubstitution unit tests can be found here. Example of attaching advice to a core Java class is found here.

The theory is that we can intercept the execution of Executable.declaredAnnotations() on exit, replacing the standard Test annotation with an instance of MutableTest. The code in MockMethodAdvice of Mockito is seemingly applicable to this scenario.

sbabcoc commented 2 years ago

Unsurprisingly, the Executable class cannot be instrumented. I think I can do this by instrumenting the FrameworkMethod class instead. This class has getAnnotations() and getAnnotation(...) methods that could be overridden to return cached values instead of forwarding the calls to their respective intercepted methods.

sbabcoc commented 2 years ago

Resolved by #119