@Test public void testCommand() throws CommandException { CommandManager cm = new CommandManager(); CommandSource testSource = new TestCommandSource(cm); Command cmd = cm.getCommand("test1") .addAlias("t1") .setPermission("test.1") .setExecutor( new Executor() { @Override public void execute(CommandSource source, Command command, CommandArguments args) throws CommandException { args.popString("testArg1"); args.popString("testArg2", "Not specified"); args.assertCompletelyParsed(); } }); // execute with success cmd.process(testSource, "foo"); cmd.process(testSource, "foo", "bar"); // execute with failure thrown.expect(CommandException.class); cmd.process(testSource, "foo", "bar", "baz"); cmd.process(testSource); }
@Override public void processCommand(String command, String... args) { Command cmd = cm.getCommand(command, false); if (cmd == null) { sendMessage("Unknown command: '" + command + "'."); return; } try { cmd.process(this, args); } catch (CommandException e) { throw new RuntimeException(e); } }
@Test public void testProcessExitCommand_thowsExitException() { // given Command command = new Exit(view); // when try { command.process("exit"); fail("Expected ExitException"); } catch (ExitException e) { // do nothing } // then Mockito.verify(view).write("До скорой встречи!"); }