### Question:
UNIXUtils { public static synchronized void createUser( final String user, @Nullable final String group, final Executor executor ) throws IOException { final CommandLine idCheckCommandLine = new CommandLine("id") .addArgument("-u") .addArgument(user); try { executor.execute(idCheckCommandLine); log.debug("User already exists"); } catch (final IOException ioe) { log.debug("User does not exist. Creating it now."); final boolean isGroupValid = StringUtils.isNotBlank(group) && !group.equals(user); if (isGroupValid) { log.debug("Group and User are different so creating group now."); final CommandLine groupCreateCommandLine = new CommandLine(SUDO).addArgument("groupadd") .addArgument(group); try { log.debug("Running command to create group: [{}]", groupCreateCommandLine); executor.execute(groupCreateCommandLine); } catch (IOException ioexception) { log.debug("Group creation threw an error as it might already exist", ioexception); } } final CommandLine userCreateCommandLine = new CommandLine(SUDO) .addArgument("useradd") .addArgument(user); if (isGroupValid) { userCreateCommandLine .addArgument("-G") .addArgument(group); } userCreateCommandLine .addArgument("-M"); log.debug("Running command to create user: [{}]", userCreateCommandLine); executor.execute(userCreateCommandLine); } } private  UNIXUtils(); static synchronized void createUser(
        final String user,
        @Nullable final String group,
        final Executor executor
    ); static void changeOwnershipOfDirectory(
        final String dir,
        final String user,
        final Executor executor
    );  }### Answer:
@Test void testCreateUserMethodSuccessAlreadyExists() throws IOException { final String user = "user"; final String group = "group"; final ArgumentCaptor<CommandLine> argumentCaptor = ArgumentCaptor.forClass(CommandLine.class); final List<String> command = Arrays.asList("id", "-u", user); UNIXUtils.createUser(user, group, executor); Mockito.verify(this.executor).execute(argumentCaptor.capture()); Assertions.assertThat(argumentCaptor.getValue().toStrings()).containsExactlyElementsOf(command); }
@Test void testCreateUserMethodSuccessDoesNotExist1() throws IOException { final String user = "user"; final String group = "group"; final CommandLine idCheckCommandLine = new CommandLine("id"); idCheckCommandLine.addArgument("-u"); idCheckCommandLine.addArgument(user); Mockito.when(this.executor.execute(Mockito.any(CommandLine.class))).thenThrow(new IOException()); final ArgumentCaptor<CommandLine> argumentCaptor = ArgumentCaptor.forClass(CommandLine.class); final List<String> command = Arrays.asList("sudo", "useradd", user, "-G", group, "-M"); try { UNIXUtils.createUser(user, group, executor); } catch (IOException ignored) { } Mockito.verify(this.executor, Mockito.times(3)).execute(argumentCaptor.capture()); Assertions.assertThat(argumentCaptor.getAllValues().get(2).toStrings()).containsExactlyElementsOf(command); }
@Test void testCreateUserMethodSuccessDoesNotExist2() throws IOException { final String user = "user"; final String group = "group"; Mockito.when(this.executor.execute(Mockito.any(CommandLine.class))).thenThrow(new IOException()); Assertions.assertThatIOException().isThrownBy(() -> UNIXUtils.createUser(user, group, executor)); }