Skip to content

Instantly share code, notes, and snippets.

@mtov
Last active December 6, 2021 13:53
Show Gist options
  • Save mtov/3601acd0b32a1d0a85b4a81a43af4284 to your computer and use it in GitHub Desktop.
Save mtov/3601acd0b32a1d0a85b4a81a43af4284 to your computer and use it in GitHub Desktop.
Teste de unidade de uma classe Stack
import java.util.ArrayList;
import java.util.EmptyStackException;
public class Stack<T> {
private ArrayList<T> elements = new ArrayList<T>();
private int size = 0;
public int size() {
return size;
}
public boolean isEmpty(){
return (size == 0);
}
public void push(T elem) {
elements.add(elem);
size++;
}
public T pop() throws EmptyStackException {
if (isEmpty())
throw new EmptyStackException();
T elem = elements.remove(size-1);
size--;
return elem;
}
}
import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
public class StackTest {
Stack<Integer> stack;
@Before
public void init() {
stack = new Stack<Integer>();
}
@Test
public void testEmptyStack() {
assertTrue(stack.isEmpty());
}
@Test
public void testNotEmptyStack() {
stack.push(10);
assertFalse(stack.isEmpty());
}
@Test
public void testSizeStack() {
stack.push(10);
stack.push(20);
stack.push(30);
int size = stack.size();
assertEquals(3,size);
}
@Test
public void testPushPopStack() {
stack.push(10);
stack.push(20);
stack.push(30);
int result = stack.pop();
result = stack.pop();
assertEquals(20,result);
}
@Test(expected = java.util.EmptyStackException.class)
public void testEmptyStackException() {
stack.push(10);
int result = stack.pop();
result = stack.pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment