How to access testRuntime classpath in a custom gradle-plugin for a java project?

I want to load the classes using custom class loader in order to use in my gradle plugin. But I don’t know how to access the testRuntime classpath (from the java plugin) in my plugin.

You can use sourceSets.test.runtimeClasspath

Thanks, but I don’t know how to add testRuntime classpath to buildscript block classpath.

You can’t add it to buildscript classpath. There’s a chicken or egg problem where buildscript classpath is defined before test classes are compiled. You’ll need to use a custom classloader

task myTask() {
   dependsOn compileTestJava
   doLast {
      URL[] urls = sourceSets.test.runtimeClasspath.files.collect { it.toUri().toUrl() } 
      Classloader cl = new URLClassloader(urls, null)
      ... 
   } 
} 

Here’s a related topic for using reflections with a classloader
https://discuss.gradle.org/t/gradle-plugin-with-reflections?source_topic_id=23779