我有一个 Spring Shell基于应用程序和几个脚本。是否有一种简单的方法可以在 JUnit 测试中运行脚本,以便在脚本执行期间发生某些异常/错误时测试失败?
测试的目的是确保所有正确的脚本都能正常运行。
更新 1:
这是一个用于在 JUnit 中运行脚本的小助手类:
import org.apache.commons.io.FileUtils;
import org.springframework.shell.Bootstrap;
import org.springframework.shell.core.CommandResult;
import org.springframework.shell.core.JLineShellComponent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.fest.assertions.api.Assertions.*;
public class ScriptRunner {
public void runScript(final File file) throws IOException
{
final Bootstrap bootstrap = new Bootstrap();
final JLineShellComponent shell = bootstrap.getJLineShellComponent();
final List<String> lines = FileUtils.readLines(file);
for (final String line : lines) {
execVerify(line, shell);
}
}
private void execVerify(final String command, final JLineShellComponent shell) {
final CommandResult result = shell.executeCommand(command);
assertThat(result.isSuccess()).isTrue();
}
}
最佳答案
您可以创建一个 Bootstrap
实例,从中获取 shell
然后 executeCommand()
(包括 shell
命令)就可以了。
您可能对 Spring XD 为此做了什么感兴趣:https://github.com/spring-projects/spring-xd/blob/master/spring-xd-shell/src/test/java/org/springframework/xd/shell/AbstractShellIntegrationTest.java (虽然有很多XD具体的细节)
关于java - 如何在 JUnit 测试中运行 Spring Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28847065/