Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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");
}
}