* PDF to X * PDF to X * Remove created doc * Code fixes and cleanup for PDF module * Fix web.xml in spring-mvc-web-vs-initializer project * Rollback web.xml * Fixes to PDF article * Merge with main eugen branch * Junit 5 * Fix name of test and modifier
82 lines
2.2 KiB
Java
82 lines
2.2 KiB
Java
package com.baeldung;
|
|
|
|
import java.util.EmptyStackException;
|
|
import java.util.Stack;
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.DisplayName;
|
|
import org.junit.jupiter.api.Nested;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
}
|