- Added code for JUnit 5 article
This commit is contained in:
31
junit5/src/test/java/com/baeldung/AssumptionTest.java
Normal file
31
junit5/src/test/java/com/baeldung/AssumptionTest.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.gen5.api.Test;
|
||||
|
||||
import static org.junit.gen5.api.Assumptions.assumeFalse;
|
||||
import static org.junit.gen5.api.Assumptions.assumeTrue;
|
||||
import static org.junit.gen5.api.Assumptions.assumingThat;
|
||||
|
||||
public class AssumptionTest {
|
||||
|
||||
@Test
|
||||
void trueAssumption() {
|
||||
assumeTrue(5 > 1);
|
||||
System.out.println("Was true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void falseAssumption() {
|
||||
assumeFalse(5 < 1);
|
||||
System.out.println("Was false");
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumptionThat() {
|
||||
String someString = "Just a string";
|
||||
assumingThat(
|
||||
someString.equals("Just a string"),
|
||||
() -> System.out.println("Assumption was correct")
|
||||
);
|
||||
}
|
||||
}
|
||||
15
junit5/src/test/java/com/baeldung/ExceptionTest.java
Normal file
15
junit5/src/test/java/com/baeldung/ExceptionTest.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.gen5.api.Test;
|
||||
|
||||
import static org.junit.gen5.api.Assertions.assertEquals;
|
||||
import static org.junit.gen5.api.Assertions.expectThrows;
|
||||
|
||||
public class ExceptionTest {
|
||||
|
||||
@Test
|
||||
void shouldThrowException() {
|
||||
Throwable exception = expectThrows(NullPointerException.class, null);
|
||||
assertEquals(exception.getClass(), NullPointerException.class);
|
||||
}
|
||||
}
|
||||
27
junit5/src/test/java/com/baeldung/FirstTest.java
Normal file
27
junit5/src/test/java/com/baeldung/FirstTest.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.gen5.api.Test;
|
||||
|
||||
import static org.junit.gen5.api.Assertions.assertAll;
|
||||
import static org.junit.gen5.api.Assertions.assertEquals;
|
||||
import static org.junit.gen5.api.Assertions.assertTrue;
|
||||
|
||||
class FirstTest {
|
||||
|
||||
@Test
|
||||
void lambdaExpressions() {
|
||||
String string = "";
|
||||
assertTrue(() -> string.isEmpty(), "String should be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupAssertions() {
|
||||
int[] numbers = {0,1,2,3,4};
|
||||
assertAll("numbers", () -> {
|
||||
assertEquals(numbers[0], 1);
|
||||
assertEquals(numbers[3], 3);
|
||||
assertEquals(numbers[4], 1);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
77
junit5/src/test/java/com/baeldung/NestedTest.java
Normal file
77
junit5/src/test/java/com/baeldung/NestedTest.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.gen5.api.*;
|
||||
|
||||
import java.util.EmptyStackException;
|
||||
import java.util.Stack;
|
||||
|
||||
public class NestedTest {
|
||||
Stack<Object> stack;
|
||||
boolean isRun = false;
|
||||
|
||||
@Test
|
||||
@DisplayName("is instantiated with new Stack()")
|
||||
void isInstantiatedWithNew() {
|
||||
new Stack<Object>();
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("when new")
|
||||
class WhenNew {
|
||||
|
||||
@BeforeEach
|
||||
void init() {
|
||||
stack = new Stack<Object>();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("is empty")
|
||||
void isEmpty() {
|
||||
Assertions.assertTrue(stack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("throws EmptyStackException when popped")
|
||||
void throwsExceptionWhenPopped() {
|
||||
Assertions.expectThrows(EmptyStackException.class, () -> stack.pop());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("throws EmptyStackException when peeked")
|
||||
void throwsExceptionWhenPeeked() {
|
||||
Assertions.expectThrows(EmptyStackException.class, () -> stack.peek());
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("after pushing an element")
|
||||
class AfterPushing {
|
||||
|
||||
String anElement = "an element";
|
||||
|
||||
@BeforeEach
|
||||
void init() {
|
||||
stack.push(anElement);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("it is no longer empty")
|
||||
void isEmpty() {
|
||||
Assertions.assertFalse(stack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("returns the element when popped and is empty")
|
||||
void returnElementWhenPopped() {
|
||||
Assertions.assertEquals(anElement, stack.pop());
|
||||
Assertions.assertTrue(stack.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("returns the element when peeked but remains not empty")
|
||||
void returnElementWhenPeeked() {
|
||||
Assertions.assertEquals(anElement, stack.peek());
|
||||
Assertions.assertFalse(stack.isEmpty());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user