Junior — Middle
What is the purpose of the AssertJ library when writing tests?
sobes.tech AI
Answer from AI
AssertJ is a library for writing readable and expressive assertions in Java tests. It provides a convenient and extensible API for checking values, collections, exceptions, and other objects, making tests more understandable and concise compared to traditional JUnit assertions.
Example of using AssertJ:
import static org.assertj.core.api.Assertions.*;
@Test
void testString() {
String actual = "Hello, AssertJ!";
assertThat(actual)
.startsWith("Hello")
.contains("AssertJ")
.endsWith("!");
}
Thus, AssertJ helps write more expressive and readable tests, facilitating error diagnosis.