Before and after Junit annotations

This commit is contained in:
Marcos Lopez Gonzalez 2018-04-06 19:26:26 +02:00
parent 266c285533
commit 311684bc57
4 changed files with 178 additions and 0 deletions

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}