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("junit5")
public class AnnotationTestExampleUnitTest {
class AnnotationTestExampleUnitTest {
@Test
public void shouldRaiseAnException() throws Exception {
void shouldRaiseAnException() {
Assertions.assertThrows(Exception.class, () -> {
throw new Exception("This is my expected exception");
});
@ -19,7 +20,7 @@ public class AnnotationTestExampleUnitTest {
@Test
@Disabled
public void shouldFailBecauseTimeout() throws InterruptedException {
void shouldFailBecauseTimeout() {
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;
public class AssertionsExampleUnitTest {
@Test
@Disabled
public void shouldFailBecauseTheNumbersAreNotEqual() {
void shouldFailBecauseTheNumbersAreNotEqual() {
Assertions.assertEquals(2, 3, "Numbers are not equal!");
}
@Test
@Disabled
public void shouldFailBecauseItsNotTrue_overloading() {
void shouldFailBecauseItsNotTrue_overloading() {
Assertions.assertTrue(() -> {
return false;
}, () -> "It's not true!");
}
@Test
public void shouldAssertAllTheGroup() {
void shouldAssertAllTheGroup() {
List<Integer> list = Arrays.asList(1, 2, 3);
Assertions.assertAll("List is not incremental", () -> Assertions.assertEquals(list.get(0)
.intValue(), 1), () -> Assertions.assertEquals(
list.get(1)
.intValue(),
2),
() -> Assertions.assertEquals(list.get(2)
.intValue(), 3));
Assertions.assertAll("List is not incremental",
() -> Assertions.assertEquals(list.get(0).intValue(), 1),
() -> Assertions.assertEquals(list.get(1).intValue(), 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.LoggerFactory;
public class BeforeAllAndAfterAllAnnotationsUnitTest {
class BeforeAllAndAfterAllAnnotationsUnitTest {
private static final Logger LOG = LoggerFactory.getLogger(BeforeAllAndAfterAllAnnotationsUnitTest.class);
@BeforeAll
public static void setup() {
static void setup() {
LOG.debug("startup - creating DB connection");
}
@AfterAll
public static void tearDown() {
static void tearDown() {
LOG.debug("closing DB connection");
}
@Test
public void simpleTest() {
void simpleTest() {
LOG.debug("simple test");
}
@Test
public void anotherSimpleTest() {
void anotherSimpleTest() {
LOG.debug("another simple test");
}
}

View File

@ -1,37 +1,37 @@
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.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
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 List<String> list;
@BeforeEach
public void init() {
@BeforeEach
void init() {
LOG.debug("startup");
list = new ArrayList<>(Arrays.asList("test1", "test2"));
}
@AfterEach
public void teardown() {
void teardown() {
LOG.debug("teardown");
list.clear();
}
@Test
public void whenCheckingListSize_ThenSizeEqualsToInit() {
void whenCheckingListSize_thenSizeEqualsToInit() {
LOG.debug("executing test");
assertEquals(2, list.size());
@ -39,7 +39,7 @@ public class BeforeEachAndAfterEachAnnotationsUnitTest {
}
@Test
public void whenCheckingListSizeAgain_ThenSizeEqualsToInit() {
public void whenCheckingListSizeAgain_thenSizeEqualsToInit() {
LOG.debug("executing another test");
assertEquals(2, list.size());

View File

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