Java Junit测试堆栈pop
如果我的堆栈工作正常,我将尝试使用JUnitIT测试。我得到了输出:Java Junit测试堆栈pop,java,junit,stack,Java,Junit,Stack,如果我的堆栈工作正常,我将尝试使用JUnitIT测试。我得到了输出: testPopEmptyStack(StackTesting.TestJunitStack): null false 然而,我希望得到一个输出true,因为在我的堆栈类中。如果pop() 堆栈类: public class Stack { Node top; int count = 0; ArrayList<Node> stack = new ArrayList<Node>()
testPopEmptyStack(StackTesting.TestJunitStack): null
false
然而,我希望得到一个输出true
,因为在我的堆栈类中。如果pop()
堆栈类:
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;
}
}
TestRunnerStack.java:
public class TestJunitStack extends TestCase{
static Stack emptystack = new Stack();
@Test(expected = EmptyStackException.class)
public void testPopEmptyStack() {
emptystack.pop();
}
}
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());
}
}
编辑
static
从这里删除testpoppentystack
从这里删除static
public static void testPopEmptyStack() {
...
输出是相同的=[尝试我的简化测试@test(预期=EmptyStackException.class)public void testPopEmptyStack(){抛出新的EmptyStackException();}嗯奇怪,它有相同的输出。我喜欢这个测试用例Fyi:我的测试输出是“真”垃圾…是什么原因造成的