Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Diferences
- ## JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
- - **JUnit Platform** serves as a foundation for launching testing frameworks on the JVM.
- - **JUnit Jupiter** is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5.
- - **JUnit Vintage** provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.
- ## Annotations
- JUnit 5 comes with important changes within its annotations. The most important one is that we can no longer use `@Test` annotation for specifying expectations.
- ### `expected` attribute can be replaced with `assertThrows`
- ```java
- @Test(expected = Exception.class)
- public void shouldRaiseAnException() throws Exception {
- // ...
- }
- ```
- becomes,
- ```java
- public void shouldRaiseAnException() throws Exception {
- Assertions.assertThrows(Exception.class, () -> {
- //...
- });
- }
- ```
- ### `timeout` attribute can be replaced with `assertTimeout`
- ```java
- @Test(timeout = 1)
- public void shouldFailBecauseTimeout() throws InterruptedException {
- Thread.sleep(10);
- }
- ```
- becomes
- ```java
- @Test
- public void shouldFailBecauseTimeout() throws InterruptedException {
- Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10));
- }
- ```
- ## import location
- | JUnit4 | JUnit5 |
- |---|---|
- | `org.junit.Test` | `org.junit.jupiter.api.Test` |
- | `org.junit.Before` | `org.junit.jupiter.api.BeforeEach` |
- | `org.junit.After` | `org.junit.jupiter.api.AfterEach` |
- | `org.junit.BeforeClass` | `org.junit.jupiter.api.BeforeAll` |
- | `org.junit.AfterClass` | `org.junit.jupiter.api.AfterAll` |
- | `org.junit.Ignore` | `org.junit.jupiter.api.Disabled` |
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement