WriteProjectFile extends AbstractCommand { @Override public void execute() throws Exception { assertTrue(1 == arguments.size()); final Path projectFilePath = Paths.get(arguments.get(0)); final ObjectMapper mapper = new ObjectMapper(); if (Files.exists(projectFilePath)) { logger.warn("Project file already exists"); return; } try (final BufferedWriter writer = newBufferedWriter(projectFilePath)) { mapper.writerWithDefaultPrettyPrinter().writeValue(writer, project); } }  @Override void execute();  }
@Test @PrepareForTest(WriteProjectFile.class) public void testExecute() throws Exception { mockStatic(Files.class); final BufferedWriter bufferedWriter = mock(BufferedWriter.class); when(Files.newBufferedWriter(any(Path.class))).thenReturn(bufferedWriter); final WriteProjectFile command = new WriteProjectFile(); command.setArguments(singletonList(randomUUID().toString())); command.execute(); verify(bufferedWriter).write(any(char[].class), anyInt(), anyInt()); }
@Test @PrepareForTest(WriteProjectFile.class) public void shouldNotCreateFile() throws Exception { mockStatic(Files.class); when(Files.exists(any())).thenReturn(true); final BufferedWriter bufferedWriter = mock(BufferedWriter.class); when(Files.newBufferedWriter(any(Path.class))).thenReturn(bufferedWriter); final WriteProjectFile command = new WriteProjectFile(); command.setArguments(singletonList(randomUUID().toString())); command.execute(); verify(bufferedWriter, new AtMost(0)).write(any(char[].class), anyInt(), anyInt()); }