Skip to content

Commit b7355cb

Browse files
araknoidpivovarit
authored andcommitted
BAEL-1843 - JUnit4 -> JUnit5 migration guide (eugenp#4816)
* Added code for the migration from JUnit 4 to JUnit 5 * Add example for Rule migration support * Add fix to access modifiers to test methods * Remove wrong header * Add junit5-migration module and its code snippets * Add module configuration to pom * Remove test classes that were added for the migration from JUnit 4 to JUnit 5 article (moved under correct module)
1 parent b5d5a49 commit b7355cb

25 files changed

Lines changed: 656 additions & 9 deletions

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@
905905
<module>json</module>
906906
<module>jsoup</module>
907907
<module>testing-modules/junit-5</module>
908+
<module>testing-modules/junit5-migration</module>
908909
<module>jws</module>
909910
<module>libraries-data</module>
910911
<module>linkrest</module>

‎testing-modules/junit-5/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<version>${junit.vintage.version}</version>
3434
<scope>test</scope>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-migrationsupport</artifactId>
39+
<version>${junit.vintage.version}</version>
40+
<scope>test</scope>
41+
</dependency>
3642
<dependency>
3743
<groupId>org.apache.logging.log4j</groupId>
3844
<artifactId>log4j-core</artifactId>

‎testing-modules/junit-5/src/test/java/com/baeldung/AssertionUnitTest.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
import java.util.function.BooleanSupplier;
1212

1313
import org.junit.jupiter.api.Disabled;
14+
import org.junit.jupiter.api.DisplayName;
1415
import org.junit.jupiter.api.Test;
1516

1617
/**
1718
* Unit test that demonstrate the different assertions available within JUnit 4
1819
*/
20+
@DisplayName("Test case for assertions")
1921
public class AssertionUnitTest {
2022

2123
@Test
24+
@DisplayName("Arrays should be equals")
2225
public void whenAssertingArraysEquality_thenEqual() {
2326
char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'};
2427
char[] actual = "Jupiter".toCharArray();
@@ -27,6 +30,7 @@ public void whenAssertingArraysEquality_thenEqual() {
2730
}
2831

2932
@Test
33+
@DisplayName("The area of two polygons should be equal")
3034
public void whenAssertingEquality_thenEqual() {
3135
float square = 2 * 2;
3236
float rectangle = 2 * 2;

‎testing-modules/junit-5/src/test/java/com/baeldung/NestedUnitTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import org.junit.jupiter.api.Test;
1111

1212
public class NestedUnitTest {
13+
1314
Stack<Object> stack;
14-
boolean isRun = false;
1515

1616
@Test
1717
@DisplayName("is instantiated with new Stack()")
1818
void isInstantiatedWithNew() {
19-
new Stack<Object>();
19+
new Stack<>();
2020
}
2121

2222
@Nested
@@ -25,7 +25,7 @@ class WhenNew {
2525

2626
@BeforeEach
2727
void init() {
28-
stack = new Stack<Object>();
28+
stack = new Stack<>();
2929
}
3030

3131
@Test

testing-modules/junit-5/src/test/java/com/baeldung/AssumptionUnitTest.java renamed to testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/AssumptionUnitTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung;
1+
package com.baeldung.migration.junit5;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assumptions.assumeFalse;
@@ -10,19 +10,19 @@
1010
public class AssumptionUnitTest {
1111

1212
@Test
13-
void trueAssumption() {
14-
assumeTrue(5 > 1);
13+
public void trueAssumption() {
14+
assumeTrue(5 > 1, () -> "5 is greater the 1");
1515
assertEquals(5 + 2, 7);
1616
}
1717

1818
@Test
19-
void falseAssumption() {
20-
assumeFalse(5 < 1);
19+
public void falseAssumption() {
20+
assumeFalse(5 < 1, () -> "5 is less then 1");
2121
assertEquals(5 + 2, 7);
2222
}
2323

2424
@Test
25-
void assumptionThat() {
25+
public void assumptionThat() {
2626
String someString = "Just a string";
2727
assumingThat(someString.equals("Just a string"), () -> assertEquals(2 + 2, 4));
2828
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Relevant Articles:
2+
- [JUnit4 -> JUnit5 migration guide](http://www.baeldung.com/junit4-junit5-migration-guide)
3+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>junit5-migration</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<name>junit5-migration</name>
10+
<description>JUnit 4 -> JUnit 5 migration</description>
11+
12+
<parent>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>parent-modules</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
16+
<relativePath>../../</relativePath>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.junit.platform</groupId>
22+
<artifactId>junit-platform-engine</artifactId>
23+
<version>${junit.platform.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.junit.platform</groupId>
27+
<artifactId>junit-platform-runner</artifactId>
28+
<version>${junit.platform.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit.vintage</groupId>
33+
<artifactId>junit-vintage-engine</artifactId>
34+
<version>${junit.vintage.version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-migrationsupport</artifactId>
40+
<version>${junit.vintage.version}</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<resources>
47+
<resource>
48+
<directory>src/test/resources</directory>
49+
<filtering>true</filtering>
50+
</resource>
51+
</resources>
52+
<plugins>
53+
<plugin>
54+
<artifactId>maven-surefire-plugin</artifactId>
55+
<version>${maven-surefire-plugin.version}</version>
56+
<dependencies>
57+
<dependency>
58+
<groupId>org.junit.platform</groupId>
59+
<artifactId>junit-platform-surefire-provider</artifactId>
60+
<version>${junit.platform.version}</version>
61+
</dependency>
62+
</dependencies>
63+
</plugin>
64+
<plugin>
65+
<groupId>org.codehaus.mojo</groupId>
66+
<artifactId>exec-maven-plugin</artifactId>
67+
<version>${exec-maven-plugin.version}</version>
68+
<executions>
69+
<execution>
70+
<goals>
71+
<goal>java</goal>
72+
</goals>
73+
</execution>
74+
</executions>
75+
<configuration>
76+
<mainClass>com.baeldung.TestLauncher</mainClass>
77+
</configuration>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
82+
<properties>
83+
<junit.jupiter.version>5.1.0</junit.jupiter.version>
84+
<junit.platform.version>1.1.0</junit.platform.version>
85+
<junit.vintage.version>5.2.0</junit.vintage.version>
86+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
87+
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
88+
</properties>
89+
90+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.junit4;
2+
3+
import com.baeldung.junit4.categories.Annotations;
4+
import com.baeldung.junit4.categories.JUnit4UnitTest;
5+
import org.junit.Ignore;
6+
import org.junit.Test;
7+
import org.junit.experimental.categories.Category;
8+
9+
@Category(value = { Annotations.class, JUnit4UnitTest.class })
10+
public class AnnotationTestExampleUnitTest {
11+
@Test(expected = Exception.class)
12+
public void shouldRaiseAnException() throws Exception {
13+
throw new Exception("This is my expected exception");
14+
}
15+
16+
@Test(timeout = 1)
17+
@Ignore
18+
public void shouldFailBecauseTimeout() throws InterruptedException {
19+
Thread.sleep(10);
20+
}
21+
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.junit4;
2+
3+
import org.junit.Assert;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.hamcrest.CoreMatchers.hasItems;
10+
import static org.junit.Assert.*;
11+
import static org.junit.Assert.assertArrayEquals;
12+
13+
@DisplayName("Test case for assertions")
14+
public class AssertionUnitTest {
15+
16+
@Test
17+
@DisplayName("Arrays should be equals")
18+
public void whenAssertingArraysEquality_thenEqual() {
19+
char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'};
20+
char[] actual = "Jupiter".toCharArray();
21+
22+
assertArrayEquals("Arrays should be equal", expected, actual);
23+
}
24+
25+
@Test
26+
public void givenMultipleAssertion_whenAssertingAll_thenOK() {
27+
assertEquals("4 is 2 times 2", 4, 2 * 2);
28+
assertEquals("java", "JAVA".toLowerCase());
29+
assertEquals("null is equal to null", null, null);
30+
}
31+
32+
@Test
33+
public void testAssertThatHasItems() {
34+
assertThat(Arrays.asList("Java", "Kotlin", "Scala"), hasItems("Java", "Kotlin"));
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.junit4;
2+
3+
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assume.assumeFalse;
8+
import static org.junit.Assume.assumeTrue;
9+
10+
public class AssumeUnitTest {
11+
12+
@Test
13+
public void trueAssumption() {
14+
assumeTrue("5 is greater the 1", 5 > 1);
15+
assertEquals(5 + 2, 7);
16+
}
17+
18+
@Test
19+
public void falseAssumption() {
20+
assumeFalse("5 is less then 1", 5 < 1);
21+
assertEquals(5 + 2, 7);
22+
}
23+
}

0 commit comments

Comments
 (0)