📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Tools and Technologies Used
- Eclipse photon (only this eclipse version supports JUnit 5)
- Maven
- JUnit 5
- JDK 8 or later
assertThrows Methods
- static T assertThrows(Class expectedType, Executable executable) - Asserts that execution of the supplied executable throws an exception of the expectedType and returns the exception.
- static T assertThrows (Class expectedType, Executable executable, String message) - Asserts that execution of the supplied executable throws an exception of the expectedType and returns the exception.
- static T assertThrows (Class expectedType, Executable executable, Supplier messageSupplier) - Asserts that execution of the supplied executable throws an exception of the expectedType and returns the exception.
assertThrows Method Example
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
public class AssertThrowsExample {
@Test
void exceptionTesting() {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("a message");
});
assertEquals("a message", exception.getMessage());
}
}
Comments
Post a Comment
Leave Comment