这是indexloc提供的服务,不要输入任何密码
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
@@ -1,5 +1,6 @@
package com.baeldung.junit5vsjunit4assertions;

import org.hamcrest.MatcherAssert;
import org.junit.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -30,7 +31,7 @@ public void whenAssertingEqualityWithMessage_thenEqual() {

@Test
public void whenAssertingArraysEquality_thenEqual() {
char[] expected = { 'J', 'u', 'n', 'i', 't' };
char[] expected = {'J', 'u', 'n', 'i', 't'};
char[] actual = "Junit".toCharArray();

assertArrayEquals(expected, actual);
Expand Down Expand Up @@ -95,7 +96,7 @@ private void methodThatShouldThrowException() {

@Test
public void testAssertThatHasItems() {
assertThat(Arrays.asList("Java", "Kotlin", "Scala"), hasItems("Java", "Kotlin"));
MatcherAssert.assertThat(Arrays.asList("Java", "Kotlin", "Scala"), hasItems("Java", "Kotlin"));
}

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
package com.baeldung.junit5vsjunit4assertions;

import static java.time.Duration.ofSeconds;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static java.time.Duration.ofSeconds;
import static java.util.Arrays.asList;
import static org.junit.jupiter.api.Assertions.*;

/**
* Unit test that demonstrate the different assertions available within JUnit 4
*/
@DisplayName("Test case for assertions")
public class Junit5AssertionsUnitTest {
class Junit5AssertionsUnitTest {

@Test
@DisplayName("Arrays should be equals")
public void whenAssertingArraysEquality_thenEqual() {
void whenAssertingArraysEquality_thenEqual() {
char[] expected = {'J', 'u', 'p', 'i', 't', 'e', 'r'};
char[] actual = "Jupiter".toCharArray();

Expand All @@ -45,15 +32,15 @@ public void whenAssertingArraysEquality_thenEqual() {

@Test
@DisplayName("The area of two polygons should be equal")
public void whenAssertingEquality_thenEqual() {
void whenAssertingEquality_thenEqual() {
float square = 2 * 2;
float rectangle = 2 * 2;

assertEquals(square, rectangle);
}

@Test
public void whenAssertingEqualityWithDelta_thenEqual() {
void whenAssertingEqualityWithDelta_thenEqual() {
float square = 2 * 2;
float rectangle = 3 * 2;
float delta = 2;
Expand All @@ -62,97 +49,97 @@ public void whenAssertingEqualityWithDelta_thenEqual() {
}

@Test
public void whenAssertingConditions_thenVerified() {
void whenAssertingConditions_thenVerified() {
assertTrue(5 > 4, "5 is greater the 4");
assertTrue(null == null, "null is equal to null");
}

@Test
public void whenAssertingNull_thenTrue() {
void whenAssertingNull_thenTrue() {
Object cat = null;

assertNull(cat, () -> "The cat should be null");
Supplier<String> messageSupplier = () -> "The cat should be null";
assertNull(cat, messageSupplier);
}

@Test
public void whenAssertingNotNull_thenTrue() {
void whenAssertingNotNull_thenTrue() {
Object dog = new Object();

assertNotNull(dog, () -> "The dog should not be null");
}

@Test
public void whenAssertingSameObject_thenSuccessfull() {
void whenAssertingSameObject_thenSuccessful() {
String language = "Java";
Optional<String> optional = Optional.of(language);

assertSame(language, optional.get());
}

@Test
public void givenBooleanSupplier_whenAssertingCondition_thenVerified() {
void givenBooleanSupplier_whenAssertingCondition_thenVerified() {
BooleanSupplier condition = () -> 5 > 6;

assertFalse(condition, "5 is not greater then 6");
}

@Test
@Disabled
public void whenFailingATest_thenFailed() {
// Test not completed
@Disabled("Test not completed")
void whenFailingATest_thenFailed() {
fail("FAIL - test not completed");
}

@Test
public void givenMultipleAssertion_whenAssertingAll_thenOK() {
void givenMultipleAssertion_whenAssertingAll_thenOK() {
Object obj = null;
assertAll(
"heading",
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
() -> assertEquals("java", "JAVA".toLowerCase()),
() -> assertEquals(obj, null, "null is equal to null")
"heading",
() -> assertEquals(4, 2 * 2, "4 is 2 times 2"),
() -> assertEquals("java", "JAVA".toLowerCase()),
() -> assertNull(obj, "obj is null")
);
}

@Test
public void givenTwoLists_whenAssertingIterables_thenEquals() {
void givenTwoLists_whenAssertingIterables_thenEquals() {
Iterable<String> al = new ArrayList<>(asList("Java", "Junit", "Test"));
Iterable<String> ll = new LinkedList<>(asList("Java", "Junit", "Test"));

assertIterableEquals(al, ll);
}

@Test
public void whenAssertingTimeout_thenNotExceeded() {
void whenAssertingTimeout_thenNotExceeded() {
assertTimeout(
ofSeconds(2),
() -> {
// code that requires less then 2 minutes to execute
Thread.sleep(1000);
}
ofSeconds(2),
() -> {
// code that requires less than 2 minutes to execute
Thread.sleep(1000);
}
);
}

@Test
public void whenAssertingTimeoutPreemptively_thenNotExceeded() {
void whenAssertingTimeoutPreemptively_thenNotExceeded() {
assertTimeoutPreemptively(
ofSeconds(2),
() -> {
// code that requires less then 2 minutes to execute
Thread.sleep(1000);
}
ofSeconds(2),
() -> {
// code that requires less than 2 minutes to execute
Thread.sleep(1000);
}
);
}

@Test
public void whenAssertingEquality_thenNotEqual() {
void whenAssertingEquality_thenNotEqual() {
Integer value = 5; // result of an algorithm

assertNotEquals(0, value, "The result cannot be 0");
}

@Test
public void whenAssertingEqualityListOfStrings_thenEqual() {
void whenAssertingEqualityListOfStrings_thenEqual() {
List<String> expected = asList("Java", "\\d+", "JUnit");
List<String> actual = asList("Java", "11", "JUnit");

Expand All @@ -162,16 +149,16 @@ public void whenAssertingEqualityListOfStrings_thenEqual() {
@Test
void whenAssertingException_thenThrown() {
Throwable exception = assertThrows(
IllegalArgumentException.class,
() -> {
throw new IllegalArgumentException("Exception message");
}
IllegalArgumentException.class,
() -> {
throw new IllegalArgumentException("Exception message");
}
);
assertEquals("Exception message", exception.getMessage());
}

@Test
public void testConvertToDoubleThrowException() {
void testConvertToDoubleThrowException() {
String age = "eighteen";
assertThrows(NumberFormatException.class, () -> {
convertToInt(age);
Expand Down