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

标签 java spring junit spring-shell

我有一个 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/

相关文章:

java - 使用 Maven 从 Ubuntu 在 Jenkins 中集成 Selenium

java - 一个事件有两个监听器?

java - Spring Boot - 在类路径资源中创建名称为 'dataSource' 的 bean 时出错

java - JUnit assertEquals 失败

java - SwingWorker同步方法队列阻塞还是什么?

java - 如何从集合中删除一个项目?

java - 在 Controller 中使用@requestBody 中的 Json 对象而不是映射到 POJO 的最佳方法

java - 使用 'OPE' =? 等语法时,字符串到数字的转换失败

java - Junit 不支持 major.minor 版本 51.0,但不支持 Web 应用程序

java - 预期有单个匹配 bean,但在 junitTest 中发现 3 : objectMapper, halObjectMapper,_halObjectMapper