Java Custom Exception

This commit is contained in:
gangadkho
2018-06-26 14:48:41 +08:00
parent 3cab703646
commit a0b0e1c705
7 changed files with 120 additions and 7 deletions
@@ -0,0 +1,29 @@
package com.baeldung.customexception;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import org.junit.Test;
public class IncorrectFileExtensionExceptionUnitTest {
@Test
public void testWhenCorrectFileExtensionGiven_ReceivesNoException() throws IncorrectFileNameException {
assertThat(FileManager.getFirstLine("correctFileNameWithProperExtension.txt")).isEqualTo("Default First Line");
}
@Test(expected = IncorrectFileExtensionException.class)
public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException {
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("src");
sBuffer.append(File.separator);
sBuffer.append("test");
sBuffer.append(File.separator);
sBuffer.append("resources");
sBuffer.append(File.separator);
sBuffer.append("correctFileNameWithoutProperExtension");
FileManager.getFirstLine(sBuffer.toString());
}
}
@@ -0,0 +1,19 @@
package com.baeldung.customexception;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class IncorrectFileNameExceptionUnitTest {
@Test(expected = IncorrectFileNameException.class)
public void testWhenIncorrectFileNameExceptionThrown_ReceivesIncorrectFileNameException() throws IncorrectFileNameException {
FileManager.getFirstLine("wrongFileName.txt");
}
@Test
public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException {
assertThat(FileManager.getFirstLine("correctFileName.txt")).isEqualTo("Default First Line");
}
}