Remove static
from here
public static void testPopEmptyStack() {
...
Question
I am trying to use Junit it test if my stack is working properly. I am getting the output:
testPopEmptyStack(StackTesting.TestJunitStack): null
false
However I expect to get an output true
because in my stack class. If pop()
a stack that has no nodes
in it, I wanted it to throw new EmptyStackException()
.
stack class:
public class Stack {
Node top;
int count = 0;
ArrayList<Node> stack = new ArrayList<Node>();
public boolean checkEmpty() {
if (count == 0) {
return false;
}
else {
return true;
}
}
public Node getTop() {
if (count > 0) {
return top;
}
else {
return null;
}
}
public void pop() {
if (count > 0) {
stack.remove(0);
count--;
}
else {
throw new EmptyStackException();
}
}
public void push(int data) {
Node node = new Node(data);
stack.add(node);
count++;
}
public int size() {
return count;
}
}
TestJunitStack.java:
public class TestJunitStack extends TestCase{
static Stack emptystack = new Stack();
@Test(expected = EmptyStackException.class)
public void testPopEmptyStack() {
emptystack.pop();
}
}
TestRunnerStack.java:
public class TestRunnerStack {
public void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunitStack.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
EDIT
static
removed from testPopEmptyStack
Solution
Remove static
from here
public static void testPopEmptyStack() {
...