Merge branch 'master' into JAVA-3592

This commit is contained in:
kwoyke
2021-11-25 09:03:54 +01:00
committed by GitHub
484 changed files with 3335 additions and 2247 deletions

View File

@@ -23,19 +23,19 @@ public class BeforeAndAfterAnnotationsUnitTest {
@Before
public void init() {
LOG.info("startup");
LOG.debug("startup");
list = new ArrayList<>(Arrays.asList("test1", "test2"));
}
@After
public void teardown() {
LOG.info("teardown");
LOG.debug("teardown");
list.clear();
}
@Test
public void whenCheckingListSize_thenSizeEqualsToInit() {
LOG.info("executing test");
LOG.debug("executing test");
assertEquals(2, list.size());
list.add("another test");
@@ -43,7 +43,7 @@ public class BeforeAndAfterAnnotationsUnitTest {
@Test
public void whenCheckingListSizeAgain_thenSizeEqualsToInit() {
LOG.info("executing another test");
LOG.debug("executing another test");
assertEquals(2, list.size());
list.add("yet another test");

View File

@@ -12,24 +12,24 @@ import org.slf4j.LoggerFactory;
public class BeforeClassAndAfterClassAnnotationsUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(BeforeClassAndAfterClassAnnotationsUnitTest.class);
@BeforeClass
public static void setup() {
LOG.info("startup - creating DB connection");
LOG.debug("startup - creating DB connection");
}
@AfterClass
public static void tearDown() {
LOG.info("closing DB connection");
LOG.debug("closing DB connection");
}
@Test
public void simpleTest() {
LOG.info("simple test");
LOG.debug("simple test");
}
@Test
public void anotherSimpleTest() {
LOG.info("another simple test");
LOG.debug("another simple test");
}
}

View File

@@ -1,18 +1,21 @@
package com.baeldung.migration.junit4;
import com.baeldung.migration.junit4.rules.TraceUnitTestRule;
import org.junit.Rule;
import org.junit.Test;
import com.baeldung.migration.junit4.rules.TraceUnitTestRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RuleExampleUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleExampleUnitTest.class);
@Rule
public final TraceUnitTestRule traceRuleTests = new TraceUnitTestRule();
@Test
public void whenTracingTests() {
System.out.println("This is my test");
LOGGER.debug("This is my test");
/*...*/
}
}

View File

@@ -1,15 +1,19 @@
package com.baeldung.migration.junit4.rules;
import java.util.ArrayList;
import java.util.List;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
public class TraceUnitTestRule implements TestRule {
private static final Logger LOGGER = LoggerFactory.getLogger(TraceUnitTestRule.class);
@Override
public Statement apply(Statement base, Description description) {
@@ -18,13 +22,13 @@ public class TraceUnitTestRule implements TestRule {
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
System.out.println("Starting test ... " + description.getMethodName());
LOGGER.debug("Starting test ... {}", description.getMethodName());
try {
base.evaluate();
} catch (Throwable e) {
errors.add(e);
} finally {
System.out.println("... test finished. " + description.getMethodName());
LOGGER.debug("... test finished. {}", description.getMethodName());
}
MultipleFailureException.assertEmpty(errors);

View File

@@ -15,21 +15,21 @@ public class BeforeAllAndAfterAllAnnotationsUnitTest {
@BeforeAll
public static void setup() {
LOG.info("startup - creating DB connection");
LOG.debug("startup - creating DB connection");
}
@AfterAll
public static void tearDown() {
LOG.info("closing DB connection");
LOG.debug("closing DB connection");
}
@Test
public void simpleTest() {
LOG.info("simple test");
LOG.debug("simple test");
}
@Test
public void anotherSimpleTest() {
LOG.info("another simple test");
LOG.debug("another simple test");
}
}

View File

@@ -23,19 +23,19 @@ public class BeforeEachAndAfterEachAnnotationsUnitTest {
@BeforeEach
public void init() {
LOG.info("startup");
LOG.debug("startup");
list = new ArrayList<>(Arrays.asList("test1", "test2"));
}
@AfterEach
public void teardown() {
LOG.info("teardown");
LOG.debug("teardown");
list.clear();
}
@Test
public void whenCheckingListSize_ThenSizeEqualsToInit() {
LOG.info("executing test");
LOG.debug("executing test");
assertEquals(2, list.size());
list.add("another test");
@@ -43,7 +43,7 @@ public class BeforeEachAndAfterEachAnnotationsUnitTest {
@Test
public void whenCheckingListSizeAgain_ThenSizeEqualsToInit() {
LOG.info("executing another test");
LOG.debug("executing another test");
assertEquals(2, list.size());
list.add("yet another test");

View File

@@ -1,19 +1,22 @@
package com.baeldung.migration.junit5;
import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(JUnitPlatform.class)
@ExtendWith(TraceUnitExtension.class)
public class RuleExampleUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleExampleUnitTest.class);
@Test
public void whenTracingTests() {
System.out.println("This is my test");
LOGGER.debug("This is my test");
/*...*/
}
}

View File

@@ -3,17 +3,21 @@ package com.baeldung.migration.junit5.extensions;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TraceUnitExtension implements AfterEachCallback, BeforeEachCallback {
private static final Logger LOGGER = LoggerFactory.getLogger(TraceUnitExtension.class);
@Override
public void beforeEach(ExtensionContext context) throws Exception {
System.out.println("Starting test ... " + context.getDisplayName());
LOGGER.debug("Starting test ... {}", context.getDisplayName());
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
System.out.println("... test finished. " + context.getDisplayName());
LOGGER.debug("... test finished. {}", context.getDisplayName());
}
}

View File

@@ -1,7 +1,10 @@
package com.baeldung.resourcedirectory;
import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.nio.file.Path;
@@ -9,6 +12,8 @@ import java.nio.file.Paths;
public class ReadResourceDirectoryUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(TraceUnitExtension.class);
@Test
public void givenResourcePath_whenReadAbsolutePathWithFile_thenAbsolutePathEndsWithDirectory() {
String path = "src/test/resources";
@@ -16,7 +21,7 @@ public class ReadResourceDirectoryUnitTest {
File file = new File(path);
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
LOGGER.debug(absolutePath);
Assert.assertTrue(absolutePath.endsWith("src" + File.separator + "test" + File.separator + "resources"));
}
@@ -26,7 +31,7 @@ public class ReadResourceDirectoryUnitTest {
String absolutePath = resourceDirectory.toFile().getAbsolutePath();
System.out.println(absolutePath);
LOGGER.debug(absolutePath);
Assert.assertTrue(absolutePath.endsWith("src" + File.separator + "test" + File.separator + "resources"));
}
@@ -38,7 +43,7 @@ public class ReadResourceDirectoryUnitTest {
File file = new File(classLoader.getResource(resourceName).getFile());
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
LOGGER.debug(absolutePath);
Assert.assertTrue(absolutePath.endsWith(File.separator + "example_resource.txt"));
}