# TestKit and Plugins

**URL:** <https://discuss.gradle.org/t/testkit-and-plugins/14726>\
**Category:** Help/Discuss\
**Created:** [February 24, 2016, 6:42pm UTC](https://discuss.gradle.org/t/testkit-and-plugins/14726 "2016-02-24T18:42:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirk\_Dittert](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@Dirk\_Dittert](https://discuss.gradle.org/u/Dirk_Dittert)\
**Post date:** [February 24, 2016, 6:42pm UTC](https://discuss.gradle.org/t/testkit-and-plugins/14726/1 "2016-02-24T18:42:38Z")

</div>

I’m wondering about writing tests for custom Gradle plugins with the Gradle TestKit. The result of the code used in the `build.gradle` file behaves differently, depending on how you apply plugins.

Why does this work:  
`plugins {id 'mycustomplugin'}`

but this does not:  
`apply plugin: 'mycustomplugin'`

The latter fails with `Plugin with id 'mycustomplugin' not found`. What is the reason for that behavior?

---

<div class="post-metadata">

**Author:** ![bmuschko](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/bmuschko/32/5458_2.png) [@bmuschko](https://discuss.gradle.org/u/bmuschko)\
**Post date:** [February 24, 2016, 7:02pm UTC](https://discuss.gradle.org/t/testkit-and-plugins/14726/2 "2016-02-24T19:02:23Z")

</div>

The notation `plugins { id 'mycustomplugin'}` only works if you inject the classpath with `GradleRunner.withPluginClasspath(Iterable<? extends File> classpath)`. See [Javadocs](https://docs.gradle.org/current/javadoc/org/gradle/testkit/runner/GradleRunner.html#withPluginClasspath(java.lang.Iterable)) for more information. Using the method requires a target Gradle version \>= 2.8.

The notation `apply plugin: 'mycustomplugin'` only works if you provide the plugin classpath via `buildscript.dependencies.classpath`. This is required if you need to test your plugin code with a target Gradle version \< 2.8.

The [user guide on TestKit](https://docs.gradle.org/current/userguide/test_kit.html) describes both methods in detail.

---

<div class="post-metadata">

**Author:** ![Dirk\_Dittert](https://avatars.discourse-cdn.com/v4/letter/d/5f9b8f/32.png) [@Dirk\_Dittert](https://discuss.gradle.org/u/Dirk_Dittert)\
**Post date:** [February 24, 2016, 7:06pm UTC](https://discuss.gradle.org/t/testkit-and-plugins/14726/3 "2016-02-24T19:06:26Z")

</div>

Thank you. I must have missed that part in the documentation.
