如何在 JUnit 测试中 运行 Spring Shell 脚本

How to run Spring Shell scripts in a JUnit test

我有一个基于 Spring Shell 的应用程序和几个脚本。有没有一种简单的方法可以 运行 JUnit 测试中的脚本,如果在脚本执行期间发生某些 exception/error ,测试就会失败?

测试的目的是确保所有正确的脚本运行没有错误。

更新 1:

这是 JUnit 中 运行ning 脚本的小帮手 class:

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具体细节)