Files
java-tutorials/testing-modules/junit-5/src/test/java/com/baeldung/ExceptionUnitTest.java
T

41 lines
1.2 KiB
Java
Raw Normal View History

2016-04-15 23:47:32 +03:00
package com.baeldung;
2016-11-14 09:45:01 +01:00
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
2016-04-15 23:47:32 +03:00
2017-12-21 22:14:27 +05:30
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.Map;
2016-11-14 09:45:01 +01:00
import org.junit.jupiter.api.Test;
2017-12-21 22:14:27 +05:30
import org.junit.jupiter.api.function.Executable;
2016-04-15 23:47:32 +03:00
2017-05-15 18:35:14 +02:00
public class ExceptionUnitTest {
2016-04-15 23:47:32 +03:00
2017-08-24 15:30:33 +03:00
@Test
void shouldThrowException() {
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> {
throw new UnsupportedOperationException("Not supported");
});
assertEquals(exception.getMessage(), "Not supported");
}
2016-11-14 09:45:01 +01:00
2017-08-24 15:30:33 +03:00
@Test
void assertThrowsException() {
String str = null;
assertThrows(IllegalArgumentException.class, () -> {
Integer.valueOf(str);
});
}
2017-12-21 22:14:27 +05:30
@Test
public void whenModifyMapDuringIteration_thenThrowExecption() {
Map<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "One");
hashmap.put(2, "Two");
Executable executable = () -> hashmap.forEach((key, value) -> hashmap.remove(1));
assertThrows(ConcurrentModificationException.class, executable);
}
2016-04-15 23:47:32 +03:00
}