2

我有一个基于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();
  }

}
4

1 回答 1

1

您可以创建一个实例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具体细节)

于 2015-03-05T09:33:00.247 回答