diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit4/BeforeAndAfterAnnotationsTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit4/BeforeAndAfterAnnotationsTest.java new file mode 100644 index 0000000000..305c7a8442 --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit4/BeforeAndAfterAnnotationsTest.java @@ -0,0 +1,54 @@ +package com.baeldung.migration.junit4; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(JUnit4.class) +public class BeforeAndAfterAnnotationsTest { + + private static final Logger LOG = LoggerFactory.getLogger(BeforeAndAfterAnnotationsTest.class); + + private List list; + + @Before + public void init() { + LOG.info("startup"); + + list = new ArrayList<>(); + list.add("test1"); + list.add("test2"); + } + + @After + public void finalize() { + LOG.info("finalize"); + + list.clear(); + } + + @Test + public void whenCheckingListSizeAtBeginning_ThenSizeEqualsToStartupSize() { + LOG.info("executing test"); + assertEquals(2, list.size()); + + list.add("another test"); + } + + @Test + public void whenCheckingListSizeAtBeginningAgain_ThenSizeEqualsToStartupSize() { + LOG.info("executing another test"); + assertEquals(2, list.size()); + + list.add("yet another test"); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit4/BeforeClassAndAfterClassAnnotationsTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit4/BeforeClassAndAfterClassAnnotationsTest.java new file mode 100644 index 0000000000..31717362ef --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit4/BeforeClassAndAfterClassAnnotationsTest.java @@ -0,0 +1,35 @@ +package com.baeldung.migration.junit4; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(JUnit4.class) +public class BeforeClassAndAfterClassAnnotationsTest { + + private static final Logger LOG = LoggerFactory.getLogger(BeforeClassAndAfterClassAnnotationsTest.class); + + @BeforeClass + public static void setup() { + LOG.info("startup - creating DB connection"); + } + + @AfterClass + public static void tearDown() { + LOG.info("closing DB connection"); + } + + @Test + public void simpleTest() { + LOG.info("simple test"); + } + + @Test + public void anotherSimpleTest() { + LOG.info("another simple test"); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/BeforeAllAndAfterAllAnnotationsTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/BeforeAllAndAfterAllAnnotationsTest.java new file mode 100644 index 0000000000..9af837dcb1 --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/BeforeAllAndAfterAllAnnotationsTest.java @@ -0,0 +1,35 @@ +package com.baeldung.migration.junit5; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(JUnitPlatform.class) +public class BeforeAllAndAfterAllAnnotationsTest { + + private static final Logger LOG = LoggerFactory.getLogger(BeforeAllAndAfterAllAnnotationsTest.class); + + @BeforeAll + public static void setup() { + LOG.info("startup - creating DB connection"); + } + + @AfterAll + public static void tearDown() { + LOG.info("closing DB connection"); + } + + @Test + public void simpleTest() { + LOG.info("simple test"); + } + + @Test + public void anotherSimpleTest() { + LOG.info("another simple test"); + } +} diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/BeforeEachAndAfterEachAnnotationsTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/BeforeEachAndAfterEachAnnotationsTest.java new file mode 100644 index 0000000000..9785303b48 --- /dev/null +++ b/testing-modules/junit-5/src/test/java/com/baeldung/migration/junit5/BeforeEachAndAfterEachAnnotationsTest.java @@ -0,0 +1,54 @@ +package com.baeldung.migration.junit5; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(JUnitPlatform.class) +public class BeforeEachAndAfterEachAnnotationsTest { + + private static final Logger LOG = LoggerFactory.getLogger(BeforeEachAndAfterEachAnnotationsTest.class); + + private List list; + + @BeforeEach + public void init() { + LOG.info("startup"); + + list = new ArrayList<>(); + list.add("test1"); + list.add("test2"); + } + + @AfterEach + public void finalize() { + LOG.info("finalize"); + + list.clear(); + } + + @Test + public void whenCheckingListSizeAtBeginning_ThenSizeEqualsToStartupSize() { + LOG.info("executing test"); + assertEquals(2, list.size()); + + list.add("another test"); + } + + @Test + public void whenCheckingListSizeAtBeginningAgain_ThenSizeEqualsToStartupSize() { + LOG.info("executing another test"); + assertEquals(2, list.size()); + + list.add("yet another test"); + } +}