|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package example; |
|
|
|
|
|
|
|
import static java.time.Duration.ofMillis; |
|
import static java.time.Duration.ofMinutes; |
|
import static org.junit.jupiter.api.Assertions.assertAll; |
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
import static org.junit.jupiter.api.Assertions.assertThrows; |
|
import static org.junit.jupiter.api.Assertions.assertTimeout; |
|
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; |
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
|
import java.util.concurrent.CountDownLatch; |
|
|
|
import example.domain.Person; |
|
import example.util.Calculator; |
|
|
|
import org.junit.jupiter.api.Tag; |
|
import org.junit.jupiter.api.Test; |
|
|
|
class AssertionsDemo { |
|
|
|
private final Calculator calculator = new Calculator(); |
|
|
|
private final Person person = new Person("Jane", "Doe"); |
|
|
|
@Test |
|
void standardAssertions() { |
|
assertEquals(2, calculator.add(1, 1)); |
|
assertEquals(4, calculator.multiply(2, 2), |
|
"The optional failure message is now the last parameter"); |
|
assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- " |
|
+ "to avoid constructing complex messages unnecessarily."); |
|
} |
|
|
|
@Test |
|
void groupedAssertions() { |
|
|
|
|
|
assertAll("person", |
|
() -> assertEquals("Jane", person.getFirstName()), |
|
() -> assertEquals("Doe", person.getLastName()) |
|
); |
|
} |
|
|
|
@Test |
|
void dependentAssertions() { |
|
|
|
|
|
assertAll("properties", |
|
() -> { |
|
String firstName = person.getFirstName(); |
|
assertNotNull(firstName); |
|
|
|
|
|
assertAll("first name", |
|
() -> assertTrue(firstName.startsWith("J")), |
|
() -> assertTrue(firstName.endsWith("e")) |
|
); |
|
}, |
|
() -> { |
|
|
|
|
|
String lastName = person.getLastName(); |
|
assertNotNull(lastName); |
|
|
|
|
|
assertAll("last name", |
|
() -> assertTrue(lastName.startsWith("D")), |
|
() -> assertTrue(lastName.endsWith("e")) |
|
); |
|
} |
|
); |
|
} |
|
|
|
@Test |
|
void exceptionTesting() { |
|
Exception exception = assertThrows(ArithmeticException.class, () -> |
|
calculator.divide(1, 0)); |
|
assertEquals("/ by zero", exception.getMessage()); |
|
} |
|
|
|
|
|
@Tag("timeout") |
|
|
|
@Test |
|
void timeoutNotExceeded() { |
|
|
|
assertTimeout(ofMinutes(2), () -> { |
|
|
|
}); |
|
} |
|
|
|
|
|
@Tag("timeout") |
|
|
|
@Test |
|
void timeoutNotExceededWithResult() { |
|
|
|
String actualResult = assertTimeout(ofMinutes(2), () -> { |
|
return "a result"; |
|
}); |
|
assertEquals("a result", actualResult); |
|
} |
|
|
|
|
|
@Tag("timeout") |
|
|
|
@Test |
|
void timeoutNotExceededWithMethod() { |
|
|
|
String actualGreeting = assertTimeout(ofMinutes(2), AssertionsDemo::greeting); |
|
assertEquals("Hello, World!", actualGreeting); |
|
} |
|
|
|
|
|
@Tag("timeout") |
|
@extensions.ExpectToFail |
|
|
|
@Test |
|
void timeoutExceeded() { |
|
|
|
|
|
assertTimeout(ofMillis(10), () -> { |
|
|
|
Thread.sleep(100); |
|
}); |
|
} |
|
|
|
|
|
@Tag("timeout") |
|
@extensions.ExpectToFail |
|
|
|
@Test |
|
void timeoutExceededWithPreemptiveTermination() { |
|
|
|
|
|
assertTimeoutPreemptively(ofMillis(10), () -> { |
|
|
|
new CountDownLatch(1).await(); |
|
}); |
|
} |
|
|
|
private static String greeting() { |
|
return "Hello, World!"; |
|
} |
|
|
|
} |
|
|
|
|
|
|