1

想象一下,我有以下 Spring Shell 命令类:

import org.springframework.shell.core.CommandMarker;

@Component
public class MyShellCommands implements CommandMarker {

    @CliCommand(value = COMMAND_RUN, help = "")
    public String run() {
        [...]
        // Something can go wrong here
        [...]
    }
}

如果方法中出现一些错误,我希望命令失败。

如何使run命令失败,即确保在命令出错的情况下,以下断言失败:

JLineShellComponent shell = ...;
final CommandResult result = shell.executeCommand("run");
assertThat(result.isSuccess()).isTrue(); // I want result.isSuccess() to be false, if run fails

?

4

2 回答 2

1

抛出运行时异常确实有效,您甚至可以通过以下方式检索它CommandResult.getException()

于 2015-05-04T11:26:42.250 回答
0

这是来自spring-shell的代码,其中 CommandResult 设置为 true 或 false。

只需寻找发生return new CommandResult(false...并尝试查看是否可以导致导致发生的任何场景。

例如,我注意到如果parseResult == null设置了 false 状态。

public CommandResult executeCommand(String line) {
    // Another command was attempted
    setShellStatus(ShellStatus.Status.PARSING);

    final ExecutionStrategy executionStrategy = getExecutionStrategy();
    boolean flashedMessage = false;
    while (executionStrategy == null || !executionStrategy.isReadyForCommands()) {
        // Wait
        try {
            Thread.sleep(500);
        } catch (InterruptedException ignore) {}
        if (!flashedMessage) {
            flash(Level.INFO, "Please wait - still loading", MY_SLOT);
            flashedMessage = true;
        }
    }
    if (flashedMessage) {
        flash(Level.INFO, "", MY_SLOT);
    }

    ParseResult parseResult = null;
    try {
        // We support simple block comments; ie a single pair per line
        if (!inBlockComment && line.contains("/*") && line.contains("*/")) {
            blockCommentBegin();
            String lhs = line.substring(0, line.lastIndexOf("/*"));
            if (line.contains("*/")) {
                line = lhs + line.substring(line.lastIndexOf("*/") + 2);
                blockCommentFinish();
            } else {
                line = lhs;
            }
        }
        if (inBlockComment) {
            if (!line.contains("*/")) {
                return new CommandResult(true);
            }
            blockCommentFinish();
            line = line.substring(line.lastIndexOf("*/") + 2);
        }
        // We also support inline comments (but only at start of line, otherwise valid
        // command options like http://www.helloworld.com will fail as per ROO-517)
        if (!inBlockComment && (line.trim().startsWith("//") || line.trim().startsWith("#"))) { // # support in ROO-1116
            line = "";
        }
        // Convert any TAB characters to whitespace (ROO-527)
        line = line.replace('\t', ' ');
        if ("".equals(line.trim())) {
            setShellStatus(Status.EXECUTION_SUCCESS);
            return new CommandResult(true);
        }
        parseResult = getParser().parse(line);
        if (parseResult == null) {
            return new CommandResult(false);
        }

        setShellStatus(Status.EXECUTING);
        Object result = executionStrategy.execute(parseResult);
        setShellStatus(Status.EXECUTION_RESULT_PROCESSING);
        if (result != null) {
            if (result instanceof ExitShellRequest) {
                exitShellRequest = (ExitShellRequest) result;
                // Give ProcessManager a chance to close down its threads before the overall OSGi framework is terminated (ROO-1938)
                executionStrategy.terminate();
            } else {
                handleExecutionResult(result);
            }
        }

        logCommandIfRequired(line, true);
        setShellStatus(Status.EXECUTION_SUCCESS, line, parseResult);
        return new CommandResult(true, result, null);
    } catch (RuntimeException e) {
        setShellStatus(Status.EXECUTION_FAILED, line, parseResult);
        // We rely on execution strategy to log it
        try {
            logCommandIfRequired(line, false);
        } catch (Exception ignored) {}
        return new CommandResult(false, null, e);
    } finally {
        setShellStatus(Status.USER_INPUT);
    }
}
于 2015-03-17T14:15:29.483 回答