File tree Expand file tree Collapse file tree
testing-modules/junit-5/src/test/java/com/baeldung/migration Expand file tree Collapse file tree 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 .ArrayList ;
6+ import java .util .List ;
7+
8+ import org .junit .After ;
9+ import org .junit .Before ;
10+ import org .junit .Test ;
11+ import org .junit .runner .RunWith ;
12+ import org .junit .runners .JUnit4 ;
13+ import org .slf4j .Logger ;
14+ import org .slf4j .LoggerFactory ;
15+
16+ @ RunWith (JUnit4 .class )
17+ public class BeforeAndAfterAnnotationsTest {
18+
19+ private static final Logger LOG = LoggerFactory .getLogger (BeforeAndAfterAnnotationsTest .class );
20+
21+ private List <String > list ;
22+
23+ @ Before
24+ public void init () {
25+ LOG .info ("startup" );
26+
27+ list = new ArrayList <>();
28+ list .add ("test1" );
29+ list .add ("test2" );
30+ }
31+
32+ @ After
33+ public void finalize () {
34+ LOG .info ("finalize" );
35+
36+ list .clear ();
37+ }
38+
39+ @ Test
40+ public void whenCheckingListSizeAtBeginning_ThenSizeEqualsToStartupSize () {
41+ LOG .info ("executing test" );
42+ assertEquals (2 , list .size ());
43+
44+ list .add ("another test" );
45+ }
46+
47+ @ Test
48+ public void whenCheckingListSizeAtBeginningAgain_ThenSizeEqualsToStartupSize () {
49+ LOG .info ("executing another test" );
50+ assertEquals (2 , list .size ());
51+
52+ list .add ("yet another test" );
53+ }
54+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit4 ;
2+
3+ import org .junit .AfterClass ;
4+ import org .junit .BeforeClass ;
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .junit .runners .JUnit4 ;
8+ import org .slf4j .Logger ;
9+ import org .slf4j .LoggerFactory ;
10+
11+ @ RunWith (JUnit4 .class )
12+ public class BeforeClassAndAfterClassAnnotationsTest {
13+
14+ private static final Logger LOG = LoggerFactory .getLogger (BeforeClassAndAfterClassAnnotationsTest .class );
15+
16+ @ BeforeClass
17+ public static void setup () {
18+ LOG .info ("startup - creating DB connection" );
19+ }
20+
21+ @ AfterClass
22+ public static void tearDown () {
23+ LOG .info ("closing DB connection" );
24+ }
25+
26+ @ Test
27+ public void simpleTest () {
28+ LOG .info ("simple test" );
29+ }
30+
31+ @ Test
32+ public void anotherSimpleTest () {
33+ LOG .info ("another simple test" );
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit5 ;
2+
3+ import org .junit .jupiter .api .AfterAll ;
4+ import org .junit .jupiter .api .BeforeAll ;
5+ import org .junit .jupiter .api .Test ;
6+ import org .junit .platform .runner .JUnitPlatform ;
7+ import org .junit .runner .RunWith ;
8+ import org .slf4j .Logger ;
9+ import org .slf4j .LoggerFactory ;
10+
11+ @ RunWith (JUnitPlatform .class )
12+ public class BeforeAllAndAfterAllAnnotationsTest {
13+
14+ private static final Logger LOG = LoggerFactory .getLogger (BeforeAllAndAfterAllAnnotationsTest .class );
15+
16+ @ BeforeAll
17+ public static void setup () {
18+ LOG .info ("startup - creating DB connection" );
19+ }
20+
21+ @ AfterAll
22+ public static void tearDown () {
23+ LOG .info ("closing DB connection" );
24+ }
25+
26+ @ Test
27+ public void simpleTest () {
28+ LOG .info ("simple test" );
29+ }
30+
31+ @ Test
32+ public void anotherSimpleTest () {
33+ LOG .info ("another simple test" );
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .migration .junit5 ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
8+ import org .junit .jupiter .api .AfterEach ;
9+ import org .junit .jupiter .api .BeforeEach ;
10+ import org .junit .jupiter .api .Test ;
11+ import org .junit .platform .runner .JUnitPlatform ;
12+ import org .junit .runner .RunWith ;
13+ import org .slf4j .Logger ;
14+ import org .slf4j .LoggerFactory ;
15+
16+ @ RunWith (JUnitPlatform .class )
17+ public class BeforeEachAndAfterEachAnnotationsTest {
18+
19+ private static final Logger LOG = LoggerFactory .getLogger (BeforeEachAndAfterEachAnnotationsTest .class );
20+
21+ private List <String > list ;
22+
23+ @ BeforeEach
24+ public void init () {
25+ LOG .info ("startup" );
26+
27+ list = new ArrayList <>();
28+ list .add ("test1" );
29+ list .add ("test2" );
30+ }
31+
32+ @ AfterEach
33+ public void finalize () {
34+ LOG .info ("finalize" );
35+
36+ list .clear ();
37+ }
38+
39+ @ Test
40+ public void whenCheckingListSizeAtBeginning_ThenSizeEqualsToStartupSize () {
41+ LOG .info ("executing test" );
42+ assertEquals (2 , list .size ());
43+
44+ list .add ("another test" );
45+ }
46+
47+ @ Test
48+ public void whenCheckingListSizeAtBeginningAgain_ThenSizeEqualsToStartupSize () {
49+ LOG .info ("executing another test" );
50+ assertEquals (2 , list .size ());
51+
52+ list .add ("yet another test" );
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments