Advertisement
Aldin_SXR

StackTest.java

Mar 6th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. package ds.stack.regular;
  2.  
  3. import static org.junit.jupiter.api.Assertions.*;
  4.  
  5. import org.junit.jupiter.api.AfterAll;
  6. import org.junit.jupiter.api.AfterEach;
  7. import org.junit.jupiter.api.BeforeAll;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10.  
  11. class StackTest {
  12.     private Stack<Integer> stack;
  13.  
  14.     @BeforeEach
  15.     void setUp() throws Exception {
  16.         stack = new Stack<Integer>();
  17.     }
  18.  
  19.     @AfterEach
  20.     void tearDown() throws Exception {
  21.         stack = null;
  22.     }
  23.  
  24.     @Test
  25.     void testChecksEmptyStack() {
  26.         assertTrue(stack.isEmpty());
  27.     }
  28.    
  29.     @Test
  30.     void testCorrectlyPushesToStack() {
  31.         stack.push(1);
  32.         stack.push(2);
  33.         assertFalse(stack.isEmpty());
  34.         assertEquals(2, stack.size());
  35.     }
  36.    
  37.     @Test
  38.     void testCorrectlyPopsFromStack() {
  39.         stack.push(3);
  40.         stack.push(4);
  41.         assertEquals(4, stack.pop());
  42.     }
  43.    
  44.     @Test
  45.     void testCorrectlyPopsAndPushes() {
  46.         stack.push(5);
  47.         stack.push(3);
  48.         stack.push(8);
  49.         assertEquals(3, stack.size());
  50.        
  51.         assertEquals(8, stack.pop());
  52.         assertEquals(3, stack.pop());
  53.         assertEquals(5, stack.pop());
  54.         assertEquals(0, stack.size());
  55.     }
  56.    
  57.     @Test
  58.     void testDoesNotPopFromEmptyStack() {
  59.         assertThrows(IndexOutOfBoundsException.class, () -> stack.pop());
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement