From c1a1f5b39345d8a617772d5e6a3406a1704d68e7 Mon Sep 17 00:00:00 2001 From: guusdewit Date: Mon, 22 May 2023 16:12:34 +0200 Subject: [PATCH] Added custom factory to readme for testing the Entry Command --- README.md | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7ff41a7..87b4189 100644 --- a/README.md +++ b/README.md @@ -138,7 +138,8 @@ class MySubCommand(private val browserUtils: BrowserUtils) : Runnable { // (2) Apart from `BrowserUtils`, you could also inject `RetContext`, which gives you information about e.g. the environment and the Git context. -**Note**: The entry command is also a `Command`, so you could also implement `Runnable` there, if you want to be able to execute it. Similarly, you could further +**Note**: The entry command is also a `Command`, so you could also implement `Runnable` there, if you want to be able to execute it. +In that case, you have to [customize the test setup below a bit](#test-setup-for-entry-command). ### Step 5 - Test your plugin @@ -154,16 +155,36 @@ If you want to write unit tests, you can use a `CommandLine` object to invoke yo class MySubCommandTest { val browserUtils = mock() - private val command = CommandLine(MySubCommand(browserUtils)) + private val commandLine = CommandLine(MySubCommand(browserUtils)) @Test fun `should open google`() { - command.execute() // you can specify command line arguments here if used by your command, like execute("how", "to", "walk") + commandLine.execute() // you can specify command line arguments here if used by your command, like execute("how", "to", "walk") verify(browserUtils).openUrl("https://www.google.com") } } ``` +#### Test Setup for Entry Command +On top of what is written above, when you are testing the implementation of your entry command, you have to provide a custom picocli `IFacotry`, as the `PluginInitializeCommand` cannot be created automatically. +To provide a simple mock, you can do the following: +```kotlin +// ... +private val commandLine = CommandLine(MyPluginEntryCommand(), CustomInitializationFactory()) +// ... + +class CustomInitializationFactory : IFactory { + private val pluginInitializeCommand: PluginInitializeCommand = mock() + override fun create(cls: Class?): K { + return if (cls?.isInstance(pluginInitializeCommand) == true) { + cls.cast(pluginInitializeCommand) + } else { + CommandLine.defaultFactory().create(cls) + } + } +} +``` + ### Step 6 - Native build To create your plugin library, it needs to be natively compiled. Add the following profile to your pom.xml