Merge pull request #13089 from hkhan/JAVA-15774-update-junit-articles

[JAVA-15774] Update JUnit code
This commit is contained in:
Loredana Crusoveanu 2022-11-29 17:28:47 +02:00 committed by GitHub
commit 0484640a06
5 changed files with 35 additions and 36 deletions

View File

@ -9,9 +9,10 @@ import org.junit.jupiter.api.Test;
@Tag("annotations") @Tag("annotations")
@Tag("junit5") @Tag("junit5")
public class AnnotationTestExampleUnitTest { class AnnotationTestExampleUnitTest {
@Test @Test
public void shouldRaiseAnException() throws Exception { void shouldRaiseAnException() {
Assertions.assertThrows(Exception.class, () -> { Assertions.assertThrows(Exception.class, () -> {
throw new Exception("This is my expected exception"); throw new Exception("This is my expected exception");
}); });
@ -19,7 +20,7 @@ public class AnnotationTestExampleUnitTest {
@Test @Test
@Disabled @Disabled
public void shouldFailBecauseTimeout() throws InterruptedException { void shouldFailBecauseTimeout() {
Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10)); Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10));
} }
} }

View File

@ -8,30 +8,28 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class AssertionsExampleUnitTest { public class AssertionsExampleUnitTest {
@Test @Test
@Disabled @Disabled
public void shouldFailBecauseTheNumbersAreNotEqual() { void shouldFailBecauseTheNumbersAreNotEqual() {
Assertions.assertEquals(2, 3, "Numbers are not equal!"); Assertions.assertEquals(2, 3, "Numbers are not equal!");
} }
@Test @Test
@Disabled @Disabled
public void shouldFailBecauseItsNotTrue_overloading() { void shouldFailBecauseItsNotTrue_overloading() {
Assertions.assertTrue(() -> { Assertions.assertTrue(() -> {
return false; return false;
}, () -> "It's not true!"); }, () -> "It's not true!");
} }
@Test @Test
public void shouldAssertAllTheGroup() { void shouldAssertAllTheGroup() {
List<Integer> list = Arrays.asList(1, 2, 3); List<Integer> list = Arrays.asList(1, 2, 3);
Assertions.assertAll("List is not incremental", () -> Assertions.assertEquals(list.get(0) Assertions.assertAll("List is not incremental",
.intValue(), 1), () -> Assertions.assertEquals( () -> Assertions.assertEquals(list.get(0).intValue(), 1),
list.get(1) () -> Assertions.assertEquals(list.get(1).intValue(), 2),
.intValue(), () -> Assertions.assertEquals(list.get(2).intValue(), 3));
2),
() -> Assertions.assertEquals(list.get(2)
.intValue(), 3));
} }
} }

View File

@ -6,27 +6,27 @@ import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class BeforeAllAndAfterAllAnnotationsUnitTest { class BeforeAllAndAfterAllAnnotationsUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(BeforeAllAndAfterAllAnnotationsUnitTest.class); private static final Logger LOG = LoggerFactory.getLogger(BeforeAllAndAfterAllAnnotationsUnitTest.class);
@BeforeAll @BeforeAll
public static void setup() { static void setup() {
LOG.debug("startup - creating DB connection"); LOG.debug("startup - creating DB connection");
} }
@AfterAll @AfterAll
public static void tearDown() { static void tearDown() {
LOG.debug("closing DB connection"); LOG.debug("closing DB connection");
} }
@Test @Test
public void simpleTest() { void simpleTest() {
LOG.debug("simple test"); LOG.debug("simple test");
} }
@Test @Test
public void anotherSimpleTest() { void anotherSimpleTest() {
LOG.debug("another simple test"); LOG.debug("another simple test");
} }
} }

View File

@ -1,37 +1,37 @@
package com.baeldung.migration.junit5; package com.baeldung.migration.junit5;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class BeforeEachAndAfterEachAnnotationsUnitTest { import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class BeforeEachAndAfterEachAnnotationsUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(BeforeEachAndAfterEachAnnotationsUnitTest.class); private static final Logger LOG = LoggerFactory.getLogger(BeforeEachAndAfterEachAnnotationsUnitTest.class);
private List<String> list; private List<String> list;
@BeforeEach @BeforeEach
public void init() { void init() {
LOG.debug("startup"); LOG.debug("startup");
list = new ArrayList<>(Arrays.asList("test1", "test2")); list = new ArrayList<>(Arrays.asList("test1", "test2"));
} }
@AfterEach @AfterEach
public void teardown() { void teardown() {
LOG.debug("teardown"); LOG.debug("teardown");
list.clear(); list.clear();
} }
@Test @Test
public void whenCheckingListSize_ThenSizeEqualsToInit() { void whenCheckingListSize_thenSizeEqualsToInit() {
LOG.debug("executing test"); LOG.debug("executing test");
assertEquals(2, list.size()); assertEquals(2, list.size());
@ -39,7 +39,7 @@ public class BeforeEachAndAfterEachAnnotationsUnitTest {
} }
@Test @Test
public void whenCheckingListSizeAgain_ThenSizeEqualsToInit() { public void whenCheckingListSizeAgain_thenSizeEqualsToInit() {
LOG.debug("executing another test"); LOG.debug("executing another test");
assertEquals(2, list.size()); assertEquals(2, list.size());

View File

@ -8,12 +8,12 @@ import org.slf4j.LoggerFactory;
import com.baeldung.migration.junit5.extensions.TraceUnitExtension; import com.baeldung.migration.junit5.extensions.TraceUnitExtension;
@ExtendWith(TraceUnitExtension.class) @ExtendWith(TraceUnitExtension.class)
public class RuleExampleUnitTest { class RuleExampleUnitTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RuleExampleUnitTest.class); private static final Logger LOGGER = LoggerFactory.getLogger(RuleExampleUnitTest.class);
@Test @Test
public void whenTracingTests() { void whenTracingTests() {
LOGGER.debug("This is my test"); LOGGER.debug("This is my test");
/*...*/ /*...*/
} }