test formatter

This commit is contained in:
Loredana Crusoveanu
2023-08-11 20:41:34 +03:00
parent db65a7002b
commit cc012c9c47
372 changed files with 2701 additions and 4007 deletions

View File

@@ -7,7 +7,7 @@ public class ArrayIndexOutOfBoundsExceptionDemo {
public static void main(String[] args) {
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
getArrayElementAtIndex(numbers, 5);
getListElementAtIndex(5);
addArrayElementsUsingLoop(numbers);

View File

@@ -1,16 +1,15 @@
package com.baeldung.exception.currentstacktrace;
public class DumpStackTraceDemo
{
public class DumpStackTraceDemo {
public static void main(String[] args) {
methodA();
}
methodA();
}
public static void methodA() {
try {
int num1 = 5/0; // java.lang.ArithmeticException: divide by zero
}
catch (Exception e) {
int num1 = 5 / 0; // java.lang.ArithmeticException: divide by zero
} catch (Exception e) {
e.printStackTrace();
}
}

View File

@@ -5,6 +5,7 @@ import java.io.FileInputStream;
import java.io.IOException;
public class CheckedExceptionExcample {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream(new File("test.txt"))) {
fis.read();

View File

@@ -1,6 +1,7 @@
package com.baeldung.exception.exceptions_vs_errors;
public class RuntimeExceptionExample {
public static void main(String[] args) {
int[] arr = new int[20];

View File

@@ -6,6 +6,7 @@ import java.util.Map;
import java.util.stream.Collectors;
public class MissingReturnStatement {
public static void main(String[] args) {
int a = -12;
int result = pow(a);
@@ -34,8 +35,7 @@ public class MissingReturnStatement {
public static Map<String, Integer> createDictionary() {
List<String> words = Arrays.asList("Hello", "World");
return words.stream()
.collect(Collectors.toMap(s -> s, s -> 1));
return words.stream().collect(Collectors.toMap(s -> s, s -> 1));
}
}

View File

@@ -2,7 +2,9 @@ package com.baeldung.exception.noenumconst;
public enum Priority {
HIGH("High"), MEDIUM("Medium"), LOW("Low");
HIGH("High"),
MEDIUM("Medium"),
LOW("Low");
private String name;

View File

@@ -17,18 +17,18 @@ public class ArrayIndexOutOfBoundsExceptionDemoUnitTest {
@Test
void givenAnArrayOfSizeFive_whenAccessedElementBeyondRange_thenShouldThrowArrayIndexOutOfBoundsException() {
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers));
() -> ArrayIndexOutOfBoundsExceptionDemo.addArrayElementsUsingLoop(numbers));
}
@Test
void givenAnArrayOfSizeFive_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() {
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5));
() -> ArrayIndexOutOfBoundsExceptionDemo.getArrayElementAtIndex(numbers, 5));
}
@Test
void givenAListReturnedByArraysAsListMethod_whenAccessedAnElementAtIndexEqualToSize_thenShouldThrowArrayIndexOutOfBoundsException() {
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5));
() -> ArrayIndexOutOfBoundsExceptionDemo.getListElementAtIndex(5));
}
}

View File

@@ -8,6 +8,6 @@ public class ErrorExampleUnitTest {
@Test
public void whenMainMethodIsRun_thenStackOverflowError() {
Assertions.assertThrows(AssertionError.class,
() -> ErrorExample.main(null));
() -> ErrorExample.main(null));
}
}

View File

@@ -5,9 +5,10 @@ import org.junit.jupiter.api.Assertions;
// Unit test for the RuntimeExceptionExample class.
public class RuntimeExceptionExampleUnitTest {
@Test
public void whenMainMethodIsRun_thenArrayIndexOutOfBoundsExceptionIsThrown() {
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> RuntimeExceptionExample.main(null));
Assertions.assertThrows(ArrayIndexOutOfBoundsException.class,
() -> RuntimeExceptionExample.main(null));
}
}

View File

@@ -12,17 +12,11 @@ public class SneakyThrowsExamplesUnitTest {
@Test
public void throwSneakyIOException_IOExceptionShouldBeThrown() {
assertThatThrownBy(() -> throwSneakyIOException())
.isInstanceOf(IOException.class)
.hasMessage("sneaky")
.hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOException");
assertThatThrownBy(() -> throwSneakyIOException()).isInstanceOf(IOException.class).hasMessage("sneaky").hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOException");
}
@Test
public void throwSneakyIOExceptionUsingLombok_IOExceptionShouldBeThrown() {
assertThatThrownBy(() -> throwSneakyIOExceptionUsingLombok())
.isInstanceOf(IOException.class)
.hasMessage("lombok sneaky")
.hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok");
assertThatThrownBy(() -> throwSneakyIOExceptionUsingLombok()).isInstanceOf(IOException.class).hasMessage("lombok sneaky").hasStackTraceContaining("SneakyThrowsExamples.throwSneakyIOExceptionUsingLombok");
}
}