### Question:
MarkupToEclipseHelpMojo extends AbstractMojo { protected void ensureFolderExists(String name, File folder, boolean createIfMissing) { if (folder.exists()) { if (!folder.isDirectory()) { throw new BuildFailureException(format("{0} exists but is not a folder: {1}", name, folder)); } return; } if (!createIfMissing) { throw new BuildFailureException(format("{0} does not exist: {1}", name, folder)); } if (!folder.mkdirs()) { throw new BuildFailureException(format("Cannot create {0}: {1}", name, folder)); } }  void execute();  }### Answer:
@Test public void ensureFolderExistsNotFolder() { File folder = mock(File.class); doReturn(true).when(folder).exists(); doReturn(false).when(folder).isDirectory(); BuildFailureException bfe = assertThrows(BuildFailureException.class, () -> markupToEclipseHelp.ensureFolderExists("test", folder, false)); assertTrue(bfe.getMessage().contains("test exists but is not a folder")); }
@Test public void ensureFolderExistsMissingNoCreate() { File folder = mock(File.class); BuildFailureException bfe = assertThrows(BuildFailureException.class, () -> markupToEclipseHelp.ensureFolderExists("test", folder, false)); assertTrue(bfe.getMessage().contains("test does not exist")); }
@Test public void ensureFolderExistsNothingToDo() { File folder = mock(File.class); doReturn(true).when(folder).exists(); doReturn(true).when(folder).isDirectory(); markupToEclipseHelp.ensureFolderExists("test", folder, false); verify(folder).exists(); verify(folder).isDirectory(); verifyNoMoreInteractions(folder); }
@Test public void ensureFolderExistsMissingCreate() { File folder = mock(File.class); doReturn(true).when(folder).mkdirs(); markupToEclipseHelp.ensureFolderExists("test", folder, true); verify(folder).exists(); verify(folder).mkdirs(); verifyNoMoreInteractions(folder); }
@Test public void ensureFolderExistsMissingCreateFails() { File folder = mock(File.class); BuildFailureException bfe = assertThrows(BuildFailureException.class, () -> markupToEclipseHelp.ensureFolderExists("test", folder, true)); assertTrue(bfe.getMessage().contains("Cannot create")); }
@Test public void ensureFolderExistsNotFolder() { File folder = mock(File.class); doReturn(true).when(folder).exists(); doReturn(false).when(folder).isDirectory(); thrown.expect(BuildFailureException.class); thrown.expectMessage("test exists but is not a folder"); markupToEclipseHelp.ensureFolderExists("test", folder, false); }
@Test public void ensureFolderExistsMissingNoCreate() { File folder = mock(File.class); thrown.expect(BuildFailureException.class); thrown.expectMessage("test does not exist"); markupToEclipseHelp.ensureFolderExists("test", folder, false); }
@Test public void ensureFolderExistsMissingCreateFails() { File folder = mock(File.class); thrown.expect(BuildFailureException.class); thrown.expectMessage("Cannot create"); markupToEclipseHelp.ensureFolderExists("test", folder, true); }