diff --git a/docs/src/reference/docbook/reference/dev-guide/dev-spring-shell.xml b/docs/src/reference/docbook/reference/dev-guide/dev-spring-shell.xml index 79f58d70..8fee889b 100644 --- a/docs/src/reference/docbook/reference/dev-guide/dev-spring-shell.xml +++ b/docs/src/reference/docbook/reference/dev-guide/dev-spring-shell.xml @@ -157,7 +157,55 @@ public class HelloWorldCommands implements CommandMarker { is returned, the shell will display its toString() representation. + +
+ Testing shell commands + To perform a test of the shell commands you can instantiate the shell inside a test case, + execute the command and then perform assertions on the return value CommandResult. + A simple base class to set this up is shown below. + public abstract class AbstractShellIntegrationTest { + + private static JLineShellComponent shell; + + @BeforeClass + public static void startUp() throws InterruptedException { + Bootstrap bootstrap = new Bootstrap(); + shell = bootstrap.getJLineShellComponent(); + } + + @AfterClass + public static void shutdown() { + shell.stop(); + } + + public static JLineShellComponent getShell() { + return shell; + } + +} + Here is an example testing the Date command + public class BuiltInCommandTests extends AbstractShellIntegrationTest { + + @Test + public void dateTest() throws ParseException { + + //Execute command + CommandResult cr = getShell().executeCommand("date"); + + //Get result + DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US); + Date result = df.parse(cr.getResult().toString()); + + //Make assertions - DateMaters is an external dependency not shown here. + Date now = new Date(); + MatcherAssert.assertThat(now, DateMatchers.within(5, TimeUnit.SECONDS, result)); + } +} + The java.lang.Class of CommandResult's getResult method will match that of the return value of + the method annotated with @CliCommand. You should cast to the appropriate type to help perform your assertions. + +
Building and running the shell diff --git a/src/test/java/org/springframework/shell/commands/AbstractShellIntegrationTest.java b/src/test/java/org/springframework/shell/commands/AbstractShellIntegrationTest.java new file mode 100644 index 00000000..7cfaccda --- /dev/null +++ b/src/test/java/org/springframework/shell/commands/AbstractShellIntegrationTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.shell.commands; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.springframework.shell.Bootstrap; +import org.springframework.shell.core.JLineShellComponent; + +/** + * Superclass for performing integration tests of shell commands. + * + * JUnit's BeforeClass and AfterClass annotations are used to start and stop the shell. + * in local mode with the default store configured to use in-memory storage. + * + * @author Mark Pollack + * + */ +public abstract class AbstractShellIntegrationTest { + + private static JLineShellComponent shell; + + @BeforeClass + public static void startUp() throws InterruptedException { + Bootstrap bootstrap = new Bootstrap(); + shell = bootstrap.getJLineShellComponent(); + } + + @AfterClass + public static void shutdown() { + shell.stop(); + } + + public static JLineShellComponent getShell() { + return shell; + } + +} diff --git a/src/test/java/org/springframework/shell/commands/BuiltInCommandTests.java b/src/test/java/org/springframework/shell/commands/BuiltInCommandTests.java index 9cc46395..5b9a7882 100644 --- a/src/test/java/org/springframework/shell/commands/BuiltInCommandTests.java +++ b/src/test/java/org/springframework/shell/commands/BuiltInCommandTests.java @@ -23,29 +23,24 @@ import java.util.concurrent.TimeUnit; import org.hamcrest.MatcherAssert; import org.junit.Test; -import org.springframework.shell.Bootstrap; import org.springframework.shell.core.CommandResult; -import org.springframework.shell.core.JLineShellComponent; import uk.co.it.modular.hamcrest.date.DateMatchers; -public class BuiltInCommandTests { +public class BuiltInCommandTests extends AbstractShellIntegrationTest { @Test public void dateTest() throws ParseException { - Bootstrap bootstrap = new Bootstrap(); - JLineShellComponent shell = bootstrap.getJLineShellComponent(); - - CommandResult cr = shell.executeCommand("date"); + //Execute command + CommandResult cr = getShell().executeCommand("date"); + //Get result DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,Locale.US); Date result = df.parse(cr.getResult().toString()); + + //Make assertions Date now = new Date(); MatcherAssert.assertThat(now, DateMatchers.within(5, TimeUnit.SECONDS, result)); - } - - - }