title: ByteBuddy agent test with gradle and JUnit5
body: <p>I've faced some weird behavior when I try to test my Java Agent written using ByteBuddy</p>
  <p>The agent intercepts annotated methods/classes and profile them, nothing complicated</p>
  <pre><code>class ByteInstrumentationConfigurer implements BootstrapConfigurer {
  
      @Override
      void init(...) {
          def instrumentation = ByteBuddyAgent.install()
          new AgentBuilder.Default()
                  .type(isAnnotatedWith(TimeProfiling.class))
                  .transform((builder, typeDescription, classLoader, module) -&gt;
                          builder.method(not(isAnnotatedWith(Generated))) //ignore groovy generated methods (getMetaClass, etc.)
                              .intercept(MethodDelegation.to(TimeProfilingInterceptor.class)))
                  .with(stdoutToLoggerWriter.withTransformationsOnly())
                  .installOn(instrumentation) 
  </code></pre>
  <p>And I've got some unit tests where I verify that interceptor method was called</p>
  <pre><code>class TimeProfilingInterceptorTest {
  
      static MockedStatic&lt;TimeProfilingInterceptor&gt; timeProfilingInterceptorMockedStatic
  
      static {
          //agent installation happens here
          new ByteInstrumentationConfigurer().init([:], [:])
          timeProfilingInterceptorMockedStatic = mockStatic(TimeProfilingInterceptor, CALLS_REAL_METHODS)
      }
      
      @Test
      void profilingEnabledClassLevelTest() {
          //some mocking for my classes here
          testCallingComponent.callClassLevelComponentMethod()
          timeProfilingInterceptorMockedStatic.verify(()-&gt;
                  TimeProfilingInterceptor.measureAndLogExecutionTime(any(),any(),any()),times(2))
  }
  </code></pre>
  <p>And now, when I start this test using from IDEA I see, that 2 my classes were transformed</p>
  <blockquote>
  <p>[Byte Buddy] TRANSFORM
  com.kmslh.manager.profiling.components.MethodLevelTimeProfilingComponent
  [sun.misc.Launcher$AppClassLoader@18b4aac2, null, Thread[main,5,main],
  loaded=false]</p>
  </blockquote>
  <blockquote>
  <p>[Byte Buddy] TRANSFORM
  com.kmslh.manager.profiling.components.ClassLevelTimeProfilingComponent
  [sun.misc.Launcher$AppClassLoader@18b4aac2, null, Thread[main,5,main],
  loaded=false]</p>
  </blockquote>
  <p>But if I run this tests using <code>gradle</code> test\build task - no classes transformed. If I output full info - i see that classloader in this case is <code>org.codehaus.groovy.runtime.callsite.CallSiteClassLoader</code>.</p>
  <blockquote>
  <p>[Byte Buddy] DISCOVERY
  com.kmslh.manager.profiling.components.ClassLevelTimeProfilingComponent$exposedMethod
  [org.codehaus.groovy.runtime.callsite.CallSiteClassLoader@3b1a332,
  null, Thread[Test worker,5,main], loaded=false] [Byte Buddy] IGNORE
  com.kmslh.manager.profiling.components.ClassLevelTimeProfilingComponent$exposedMethod
  [org.codehaus.groovy.runtime.callsite.CallSiteClassLoader@3b1a332,
  null, Thread[Test worker,5,main], loaded=false] [Byte Buddy] COMPLETE
  com.kmslh.manager.profiling.components.ClassLevelTimeProfilingComponent$exposedMethod
  [org.codehaus.groovy.runtime.callsite.CallSiteClassLoader@3b1a332,
  null, Thread[Test worker,5,main], loaded=false]</p>
  </blockquote>
  <p>Not sure is it an issue or not, but I'm out of ideas. Also I've tried to rewrite test to Java - no result.</p>
  <p>Any suggestions to try?</p>
  <p><strong>UPD 1</strong></p>
  <p>From what I see - when test started from gradle - no <code>auxiliary</code> classes created</p>
  <p><strong>UPD 2</strong></p>
  <p>I run gradle task with <code>-verbose:class</code> and see, that my class loaded before starting instrumentation
  <code>[Loaded com.kmslh.manager.profiling.components.ClassLevelTimeProfilingComponent from file:/C:/Users/.../build/classes/groovy/main/] </code></p>
  <p>When IDEA run tests (it packs it into jar and run jar) - class is loaded after instrumentation</p>
  <p><strong>UPD 3</strong></p>
  <blockquote>
  <p>[Loaded com....ClassLevelTimeProfilingComponent from
  file:/C:/Users/../build/classes/groovy/test/]</p>
  <p>[Loaded com....ClassLevelTimeProfilingComponent from
  <strong>VM_RedefineClasses</strong>]</p>
  <p>[Byte Buddy] TRANSFORM com....ClassLevelTimeProfilingComponent
  [sun.misc.Launcher$AppClassLoader@73d16e93, null, Thread[Test
  worker,5,main], loaded=true]</p>
  <p>[Loaded com....ClassLevelTimeProfilingComponent$exposedMethod from
  file:/C:/Users/79270/IdeaProjects/kms-trunk/KMS-Manager/build/classes/groovy/test/]</p>
  </blockquote>