File tree Expand file tree Collapse file tree
src/test/java/com/baeldung/migration Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2121 <java .version>1.8</java .version>
2222 <junit .jupiter.version>5.0.0-M4</junit .jupiter.version>
2323 <junit .platform.version>1.0.0-M4</junit .platform.version>
24+ <junit .vintage.version>4.12.0-M4</junit .vintage.version>
25+ <junit4 .version>4.12</junit4 .version>
2426
2527 <maven-compiler-plugin .version>3.6.0</maven-compiler-plugin .version>
2628 <maven-surefire-plugin .version>2.19.1</maven-surefire-plugin .version>
5153 </build >
5254
5355 <dependencies >
56+
57+ <dependency >
58+ <groupId >junit</groupId >
59+ <artifactId >junit</artifactId >
60+ <version >${junit4.version} </version >
61+ <scope >test</scope >
62+ </dependency >
63+
64+ <dependency >
65+ <groupId >org.junit.jupiter</groupId >
66+ <artifactId >junit-jupiter-api</artifactId >
67+ <version >${junit.jupiter.version} </version >
68+ <scope >test</scope >
69+ </dependency >
70+
5471 <dependency >
5572 <groupId >org.junit.jupiter</groupId >
5673 <artifactId >junit-jupiter-engine</artifactId >
5774 <version >${junit.jupiter.version} </version >
5875 <scope >test</scope >
5976 </dependency >
77+
78+ <dependency >
79+ <groupId >org.junit.platform</groupId >
80+ <artifactId >junit-platform-runner</artifactId >
81+ <version >${junit.platform.version} </version >
82+ <scope >test</scope >
83+ </dependency >
84+
85+ <!-- <dependency>
86+ <groupId>org.junit.vintage</groupId>
87+ <artifactId>junit-vintage-engine</artifactId>
88+ <version>${junit.vintage.version}</version>
89+ <scope>test</scope>
90+ </dependency> -->
6091 </dependencies >
6192</project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit4 ;
2+
3+ import org .junit .Ignore ;
4+ import org .junit .Test ;
5+ import org .junit .experimental .categories .Category ;
6+
7+ import com .baeldung .migration .junit4 .categories .Annotations ;
8+ import com .baeldung .migration .junit4 .categories .JUnit4Tests ;
9+
10+ @ Category (value = { Annotations .class , JUnit4Tests .class })
11+ public class AnnotationTestExampleTest {
12+ @ Test (expected = Exception .class )
13+ public void shouldRaiseAnException () throws Exception {
14+ throw new Exception ("This is my expected exception" );
15+ }
16+
17+ @ Test (timeout = 1 )
18+ @ Ignore
19+ public void shouldFailBecauseTimeout () throws InterruptedException {
20+ Thread .sleep (10 );
21+ }
22+
23+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit4 ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .util .Arrays ;
6+ import java .util .List ;
7+
8+ import org .junit .Ignore ;
9+ import org .junit .Test ;
10+
11+ public class AssertionsExampleTest {
12+ @ Test
13+ @ Ignore
14+ public void shouldFailBecauseTheNumbersAreNotEqualld () {
15+ assertEquals ("Numbers are not equal!" , 2 , 3 );
16+ }
17+
18+ @ Test
19+ public void shouldAssertAllTheGroup () {
20+ List <Integer > list = Arrays .asList (1 , 2 , 3 );
21+ assertEquals ("List is not incremental" , list .get (0 )
22+ .intValue (), 1 );
23+ assertEquals ("List is not incremental" , list .get (1 )
24+ .intValue (), 2 );
25+ assertEquals ("List is not incremental" , list .get (2 )
26+ .intValue (), 3 );
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit4 ;
2+
3+ import org .junit .Rule ;
4+ import org .junit .Test ;
5+
6+ import com .baeldung .migration .junit4 .rules .TraceUnitTestRule ;
7+
8+ public class RuleExampleTest {
9+
10+ @ Rule
11+ public final TraceUnitTestRule traceRuleTests = new TraceUnitTestRule ();
12+
13+ @ Test
14+ public void whenTracingTests () {
15+ System .out .println ("This is my test" );
16+ /*...*/
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit4 .categories ;
2+
3+ public interface Annotations {
4+
5+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit4 .categories ;
2+
3+ public interface JUnit4Tests {
4+
5+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit4 .rules ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ import org .junit .rules .TestRule ;
7+ import org .junit .runner .Description ;
8+ import org .junit .runners .model .MultipleFailureException ;
9+ import org .junit .runners .model .Statement ;
10+
11+ public class TraceUnitTestRule implements TestRule {
12+
13+ @ Override
14+ public Statement apply (Statement base , Description description ) {
15+
16+ return new Statement () {
17+ @ Override
18+ public void evaluate () throws Throwable {
19+ List <Throwable > errors = new ArrayList <Throwable >();
20+
21+ System .out .println ("Starting test ... " + description .getMethodName ());
22+ try {
23+ base .evaluate ();
24+ } catch (Throwable e ) {
25+ errors .add (e );
26+ } finally {
27+ System .out .println ("... test finished. " + description .getMethodName ());
28+ }
29+
30+ MultipleFailureException .assertEmpty (errors );
31+ }
32+ };
33+ }
34+
35+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit5 ;
2+
3+ import java .time .Duration ;
4+
5+ import org .junit .jupiter .api .Assertions ;
6+ import org .junit .jupiter .api .Disabled ;
7+ import org .junit .jupiter .api .Tag ;
8+ import org .junit .jupiter .api .Test ;
9+ import org .junit .platform .runner .JUnitPlatform ;
10+ import org .junit .runner .RunWith ;
11+
12+ @ Tag ("annotations" )
13+ @ Tag ("junit5" )
14+ @ RunWith (JUnitPlatform .class )
15+ public class AnnotationTestExampleTest {
16+ @ Test
17+ public void shouldRaiseAnException () throws Exception {
18+ Assertions .assertThrows (Exception .class , () -> {
19+ throw new Exception ("This is my expected exception" );
20+ });
21+ }
22+
23+ @ Test
24+ @ Disabled
25+ public void shouldFailBecauseTimeout () throws InterruptedException {
26+ Assertions .assertTimeout (Duration .ofMillis (1 ), () -> Thread .sleep (10 ));
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit5 ;
2+
3+ import java .util .Arrays ;
4+ import java .util .List ;
5+
6+ import org .junit .jupiter .api .Assertions ;
7+ import org .junit .jupiter .api .Disabled ;
8+ import org .junit .jupiter .api .Test ;
9+ import org .junit .platform .runner .JUnitPlatform ;
10+ import org .junit .runner .RunWith ;
11+
12+ @ RunWith (JUnitPlatform .class )
13+ public class AssertionsExampleTest {
14+ @ Test
15+ @ Disabled
16+ public void shouldFailBecauseTheNumbersAreNotEqual () {
17+ Assertions .assertEquals (2 , 3 , "Numbers are not equal!" );
18+ }
19+
20+ @ Test
21+ @ Disabled
22+ public void shouldFailBecauseItsNotTrue_overloading () {
23+ Assertions .assertTrue (() -> {
24+ return false ;
25+ }, () -> "It's not true!" );
26+ }
27+
28+ @ Test
29+ public void shouldAssertAllTheGroup () {
30+ List <Integer > list = Arrays .asList (1 , 2 , 3 );
31+
32+ Assertions .assertAll ("List is not incremental" , () -> Assertions .assertEquals (list .get (0 )
33+ .intValue (), 1 ),
34+ () -> Assertions .assertEquals (list .get (1 )
35+ .intValue (), 2 ),
36+ () -> Assertions .assertEquals (list .get (2 )
37+ .intValue (), 3 ));
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit5 ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .junit .jupiter .api .extension .ExtendWith ;
5+ import org .junit .platform .runner .JUnitPlatform ;
6+ import org .junit .runner .RunWith ;
7+
8+ import com .baeldung .migration .junit5 .extensions .TraceUnitExtension ;
9+
10+ @ RunWith (JUnitPlatform .class )
11+ @ ExtendWith (TraceUnitExtension .class )
12+ public class RuleExampleTest {
13+
14+ @ Test
15+ public void whenTracingTests () {
16+ System .out .println ("This is my test" );
17+ /*...*/
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments